我如何重复多个输入,直到您得到指定的答案?

2024-06-26 13:24:34 发布

您现在位置:Python中文网/ 问答频道 /正文

目前正在执行csnewbs扩展任务1(https://www.csnewbs.com/python-extended-task-1),我一直在努力处理代码的一部分,这部分代码需要3个输入,并确认它们是正确的。我有3个输入,但仅此而已

print("Hello there, welcome to Pete Porker")

while True:
    e = int(input("Scotch eggs are 30p each. How many would you like to order?"))
    if e == "":
        continue
    p = int(input("Pork Pies are 80p each. How many would you like to order?"))
    if p == "":
        continue
    q = int(input("Quiche Tarts are £1.40 each. How many would you like to order?"))
    if q == "":
        continue
print("You have ordered",e,"eggs",p,"pies and",q,"quiches.")
order = input("Is this the right order?")
if order == "yes":
    continue
elif order == "no":
    break

最后有一个“continue not property in loop”,我也不知道如何解决这个问题。如果有帮助的话,在链接的末尾有一个脚本应该运行的图像。提前感谢所有回复:)


Tags: to代码youinputiforderaremany
2条回答

如果break==no,则需要继续,否则需要break(因此这与您所做的相反)。您的最后一个if-then-else语句没有正确识别(它应该是再向右一级)。您也无法检查整数是否==“”

另外,如果输入的金额为空,我不会循环整个循环。。。我会写一个简单的函数,它有一个内部循环,不断地问相同的问题,直到得到任何非空的答案。。。差不多

def query(txt):
    while True:
        r = input(txt)
        if len(r) > 0:
            break
    return int(r)

然后你的主循环看起来像

while True:
    e=query('enter n of eggs')
    p=query('enter n of pies')
    q=query('enter n of quiche')
    A=input('is this enough? (y/n)')
    if A=='y':
        break

你的缩进消失了。缩进最后一个if/elif语句,使其位于while循环中,因为现在最后一个continue(和break)不包含在任何循环中

相关问题 更多 >