Python 我怎么回圈
我已经在网上尝试和阅读了2个多小时,但没有成功。请帮忙!如何以不固定的次数循环回代码第6行(见下文)?我知道这与while循环有关,但我真的不确定。该代码是为某人借出一定数量的一本书而设计的,但我不知道如何允许他们借出多本书。如果可能的话,请帮忙。我知道这个网站不是用来分配任务的,但我在这里真的很挣扎,如果我能得到帮助,我会知道未来。这是我的代码,不是很准确,但我是一个学习的学生Python 我怎么回圈,python,Python,我已经在网上尝试和阅读了2个多小时,但没有成功。请帮忙!如何以不固定的次数循环回代码第6行(见下文)?我知道这与while循环有关,但我真的不确定。该代码是为某人借出一定数量的一本书而设计的,但我不知道如何允许他们借出多本书。如果可能的话,请帮忙。我知道这个网站不是用来分配任务的,但我在这里真的很挣扎,如果我能得到帮助,我会知道未来。这是我的代码,不是很准确,但我是一个学习的学生 task=input("Enter 'b' to borrow a book, press 'x' to exit.
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
散列显示了我希望发生的事情。提前感谢,我很抱歉让人讨厌您可以将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
将第11行(最后2行除外)的所有内容缩进另一个级别,使其位于
while
块内。Python是布局敏感的,缩进指示块。好的就可以了。。。。。