用户输入错误值后如何制作python循环程序?

How to make python loop program after the user puts in incorrect value?

为 class 赋值,我需要用户输入 1 到 500 之间的值,如果用户输入错误值超过 3 次,则程序应该关闭。如果用户需要重试,我也会遇到使程序循环的问题

这是我目前的情况。

    flour = 2.75
    baking_soda = 1
    baking_powder = 0.5
    butter = 1
    sugar = 1.5
    egg = 1
    vanilla = 1
    makes = 48
    
    cookies = float(input('Enter number of cookies: '))
    
    count = 0
    
    flour = (cookies * flour) / makes
    baking_soda = (cookies * baking_soda) / makes
    baking_powder = (cookies * baking_powder) / makes
    butter = (cookies * butter) / makes
    sugar = (cookies * sugar) / makes
    egg = (cookies * egg) / makes
    vanilla = (cookies * vanilla) / makes
    
    while (cookies >=1 and cookies <=500):
        print("You need " + str(round(flour,2)) +
          " cups of flour, " + str(round(baking_soda,2)) +
          " teaspoons of baking soda, " + str(round(baking_powder,2)) +
          " teaspoons of baking powder, " + str(round(butter,2)) +
          " cups of butter, "+ str(round(sugar,2)) +
          " cups of sugar, " + str(round(egg,2)) +
          " eggs and " + str(round(vanilla,2)) +
          " teaspoons of vanilla.")
        break
count=0
    while (cookies <1 or cookies >500):
        count+=1
        cookies = float(input('Enter valid number: '))
        if (count>=3):
            print("I will now close.")
            exit()

这是现在的样子,我认为这就是答案,因为它不再给我带来任何问题。

flour = 2.75
baking_soda = 1
baking_powder = 0.5
butter = 1
sugar = 1.5
egg = 1
vanilla = 1
makes = 48

cookies = int(input('Enter number of cookies from 1 to 500: '))
count=0
while (cookies <1 or cookies >500):
    count+=1
    cookies = int(input('Enter valid number: '))
    if (count>=3):
        print("I will now close.")
        exit()
while (cookies >=1 and cookies <=500):
    flour = (cookies * flour) / makes
    baking_soda = (cookies * baking_soda) / makes
    baking_powder = (cookies * baking_powder) / makes
    butter = (cookies * butter) / makes
    sugar = (cookies * sugar) / makes
    egg = (cookies * egg) / makes
    vanilla = (cookies * vanilla) / makes
    
    print("You need " + str(round(flour,2)) +
      " cups of flour, " + str(round(baking_soda,2)) +
      " teaspoons of baking soda, " + str(round(baking_powder,2)) +
      " teaspoons of baking powder, " + str(round(butter,2)) +
      " cups of butter, "+ str(round(sugar,2)) +
      " cups of sugar, " + str(round(egg,2)) +
      " eggs and " + str(round(vanilla,2)) +
      " teaspoons of vanilla.")
    break

您的想法是正确的,但您需要根据循环的流程来考虑。要求用户输入总是很棘手,你总是想在进行任何计算或工作之前尝试验证输入。

有些事情你似乎已经明白了,你至少需要问一次。您可以像下面这样在单个循环中完成所有这些操作。

我基本上是在创建一个无限循环,只有当你出错 5 次时才会停止。请注意,除非用户至少正确一次,否则循环将不会继续进行计算。请注意,在现实世界中,您会为用户提供一个随意退出的机会。尝试以某种方式实现它,以便如果用户输入字母 q,程序将结束。

请注意,我留下了小错误供您修复。如果你能解决它,你就会得到循环句柄。尝试输入 2 个 cookie,然后输入 48 个 cookie。看看这个数字怎么没意义,有几种方法可以解决这个问题,但关键是要了解它为什么会出错。

flour = 2.75
baking_soda = 1
baking_powder = 0.5
butter = 1
sugar = 1.5
egg = 1
vanilla = 1
makes = 48

count = 0

while (True):
    cookies = float(input('Enter number of cookies: '))
    
    if (cookies < 1 or cookies > 500):
        print('Invalid response, the number must be between 1 and 500')
        count += 1
        
        if count > 4:
            print("I will now close.")
            break

        continue
    
    flour = (cookies * flour) / makes
    baking_soda = (cookies * baking_soda) / makes
    baking_powder = (cookies * baking_powder) / makes
    butter = (cookies * butter) / makes
    sugar = (cookies * sugar) / makes
    egg = (cookies * egg) / makes
    vanilla = (cookies * vanilla) / makes
    
    print("You need " + str(round(flour,2)) +
      " cups of flour, " + str(round(baking_soda,2)) +
      " teaspoons of baking soda, " + str(round(baking_powder,2)) +
      " teaspoons of baking powder, " + str(round(butter,2)) +
      " cups of butter, "+ str(round(sugar,2)) +
      " cups of sugar, " + str(round(egg,2)) +
      " eggs and " + str(round(vanilla,2)) +
      " teaspoons of vanilla.")