0

为班级做作业,我需要用户输入一个介于 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()
4

2 回答 2

2

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

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
于 2021-10-31T01:10:05.253 回答
1

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

有些事情你似乎已经理解了,你需要至少问一次。您可以在单个循环中完成所有这些操作,如下所示。

我基本上是在创建一个无限循环,只有当你弄错 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.")
 
于 2021-10-31T01:14:12.393 回答