我相当落后于我的课程,我停留在发展我的代码任务2 我的程序必须允许用户输入一系列的条形码,搜索文本文件的代码,然后如果找到,则询问用户他们希望购买的产品的数量。最后它应该打印收到的总产品和总价格。搜索文本文件的字符串
但我的代码不会当我在我的文件中搜索代码的工作,它只是不断循环,并要求用户重新输入其条码。
这是到目前为止我的代码:
loop=True
while loop==True:
print ("------STOCK LIST------")
print ("a - Place an order by barcode")
print ("b - Place an order by product name")
print ("x - Exit")
task=input("Please make your selection\n")
if task.lower()=="a":
print("------STOCK FILE LOADING------")
myfile=open("barcode.txt", "r") #this opens the text file
details=myfile.readlines() #reads the file and stores it as the variable 'details' #myfile.close() #closes the file
while True:
digits=input("Please enter your GTIN-8 code\n")
if len(digits) > 8 or len (digits) < 8: #if the digits are longer or shorter than 8 digits, the code is not accepted
print("Please enter a GTIN-8 code\n")
else:
break #if the code is the correct length, the loop ends
for line in details:
if digits in line:
productline=line
myfile=open("receipt.txt", "r") #opens receipt file
myfile.writelines("\n" + "+")
quantity=input("How much of the product do you wish to purchase?\n")
itemsplit=itemline.split(' ') #seperates into different words
price=float(itemsplit[2]) #price is
total=(price)*(quantity) #this works out the price
myfile.writelines("Your total spent on this product is: " +str("£:,.2f)".format(total)+"\n"))
else:
break
如果你能帮助我,我将非常感激因为没有我的同学都会帮助我,如果是这样,你可以保持的代码,因为我很简单不是最好的编码?
当你检查条形码是正确长度的突破后,你跳出while循环也就是说你的代码的其余部分是无法运行 –