我一直在尝试和在线阅读超过2个小时,但没有成功。请帮忙!如何循环回到此代码的第6行(见下文)一段不固定的时间?我知道它与while循环有关,但我真的只是不确定。该代码是专为某人借出一定数量的书籍/书籍而设计的,但我不知道如何允许他们借出多本书。请尽可能帮忙。我知道这个网站不适合作业,但我真的在这里苦苦挣扎,如果能得到帮助,我会知道未来。这是我的代码,不是很准确,但我是学生学习。
task=input("Enter 'b' to borrow a book, press 'x' to exit. \n")
if task.lower()== "b":
myfile=open("books.txt", "r+")
details=myfile.readlines()
while True:
book=input("Enter the 8 digit book code. \n")
if len(book) !=8
print("Your code is not 8 digits long, please try again.\n")
else:
break
f = open("books.txt", "r+") #I have a file with all the books, their codes and prices
for line in f.readlines():
quantity=input("How many copies of the book do you wish to purchase?\n")
t = line.split(" ")
price = float(t[3])
code = t[1]
NameOfBook = t[2]
total=(price)* int(quantity)
# Ask them if they'd like to purchase more books, if so I would like to then direct them back to "book=input("Enter the 8 digit book code. \n")"
print ("Your receipt is:",digits,"," ,name, name2) #How can I repeat this for however many books they'd like to purchase?
print ("Your total is £",total) #Then add the total cost of everything together and print it
哈希显示了我想要发生的事情。提前谢谢,我很抱歉生气,
答案 0 :(得分:0)
您可以将for循环代码放在else语句下,然后在末尾添加另一个语句,询问用户是否想要完成另一个事务。如果是这样,继续,否则,休息。您可以这样做:
task=input("Enter 'b' to borrow a book, press 'x' to exit. \n")
if task.lower()== "b":
myfile=open("books.txt", "r+")
details=myfile.readlines()
while True:
book=input("Enter the 8 digit book code. \n")
if len(book) !=8
print("Your code is not 8 digits long, please
try again.\n")
else:
f = open("books.txt", "r+")
for line in f.readlines():
quantity=input("How many copies of the book do
you wish to purchase?\n")
t = line.split(" ")
price = float(t[3])
code = t[1]
NameOfBook = t[2]
total=(price)* int(quantity)
print ("Your receipt is:",digits,"," ,name,
name2)
print ("Your total is £",total)
answer = input("Do you want to enter another book?")
if answer == "yes":
continue
else:
break