简体   繁体   English

while 循环无法正常工作,我需要使用其他东西吗?

[英]while loop not working properly, do I need to use something else?

I need to get the size of a pizza, and return it.我需要得到一个比萨饼的大小,然后将其退回。 I am using a while loop (not sure if I need to use something else?) and the problem is, even when I enter a valid choice, it prompts me to enter the size again instead of returning the size.我正在使用 while 循环(不确定是否需要使用其他东西?),问题是,即使我输入了有效的选择,它也会提示我再次输入大小而不是返回大小。

def pizzaSize():
   print("What size of pizza do you want\n (s)mall, (m)edium, or (l)arge?\n")
   size = input()
   while size != 'l' and size != 'm' and size != 's':
      print("ERROR invalid pizza size")
      size = input()
   return size`

I am pretty new to this, just started my first programming class.我对此很陌生,刚刚开始我的第一堂编程课。

Edit:编辑:

I believe what is causing my issue is another piece of the code.我相信导致我的问题的是另一段代码。 I was calling the function multiple times and causing it to repeat.我多次调用该函数并导致它重复。 Now I have a separate issue.现在我有一个单独的问题。 How do I take what is returned from a previous function and use it in a later one without calling the previous function?如何在不调用前一个函数的情况下获取从前一个函数返回的内容并在后一个函数中使用它?

def pizzaPrice():
if size == "s":
    cost = 9 + .50 * numTop
elif size == "m":
    cost = 11 + .75 * numTop
elif size == "l":
    cost = 13 + 1 * numTop
return cost

numTop is what is returned in a previous function. numTop 是在前一个函数中返回的内容。 How do I get that in the pizzaPrice function?我如何在pizzaPrice 函数中得到它? It says it is not defined, but when I define it as the function, it calls that function.它说它没有定义,但是当我将它定义为函数时,它会调用该函数。

Your code seemed to run fine for me.你的代码对我来说似乎运行良好。 It might be overkill but you can try to the following code.这可能有点矫枉过正,但您可以尝试以下代码。 I am pretty new to programming too, but I have seen other people use True and False when using while loops.我对编程也很陌生,但我看到其他人在使用 while 循环时使用 True 和 False。 I hope this helps!我希望这有帮助! Best of luck!!祝你好运!!

**EDIT I saw that you posted an additional question so I hope that this helps too. **编辑我看到你发布了一个额外的问题,所以我希望这也有帮助。 I am not sure how you are getting the "numtop" numbers but if its through a input() like the pizza size then be sure to specify it as a float since you will be using it with multiplication (inputs default as strings).我不确定你是如何获得“numtop”数字的,但如果它是通过 input() 像比萨大小那么一定要把它指定为浮点数,因为你将使用乘法(输入默认为字符串)。 In the pizzaPrice() function you need to specify that the size and numtop variables are the results of the numbofTops() and pizzaSize() functions.在pizzaPrice() 函数中,您需要指定size 和numtop 变量是numbofTops() 和pizzaSize() 函数的结果。

def numbofTops():


     numTop = float(input("how many toppings? "))
     return (numTop)


def pizzaSize():
    validAnswer = False
    print("What size of pizza do you want\n (s)mall, (m)edium, or (l)arge?\n")
    size = input("What size of pizza do you want\n (s)mall, (m)edium, or (l)arge?\n")

    while not validAnswer:
        if size != 'l' and size != 'm' and size != 's':
            print("ERROR invalid pizza size ")
            size = input()
        else:
            validAnswer = True

            return size


def pizzaPrice():
    numTop = numbofTops()
    size = pizzaSize()

    if size == "s":
        cost = 9 + .50 * numTop
        print(cost)

    elif size  == "m":
        cost = 11 + .75 * numTop
        print(cost)

    elif size  == "l":
        cost = 13 + 1 * numTop
        print(cost)
    return cost


pizzaPrice()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM