2

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.

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. How do I get that in the pizzaPrice function? It says it is not defined, but when I define it as the function, it calls that function.

Ben
  • 21
  • 2
  • You need to call the function `pizzaSize()` to get the value. Outside of your function definition call it and save the return value in a variable `size = pizzaSize()` – Ritesh Mar 14 '20 at 02:05
  • I am not able to replicate this. Calling `pizzaSlice()` and typing `s` results in the function returning as expected. Have you checked what `size` contains when your unexpected reprompts for input? – Brian61354270 Mar 14 '20 at 02:13
  • I'm not sure what you mean exactly, Brian. How would I do this? Also, I discovered that if you answer it multiple times with l, s, or m, it leads you back to a different function from earlier in the program, but when you answer that one it completes the program as needed. Not sure what is going on. – Ben Mar 14 '20 at 02:22
  • Most of the code, including `pizzaSize`, is irrelevant to the problem. You need to provide a [mre]. Once you pare it down, hopefully the issue will be obvious. Hint: it's in `pizzaPrice`. BTW welcome to SO! Check out the [tour] and [ask]. – wjandrea Mar 14 '20 at 02:41
  • wjandrea -- Thank you, I realized what the problem is but I am having a hard time solving the problem in pizzaPrice now. I updated the question. I need the returned value for my first function to be used in the pizzaPrice function, but I am having trouble doing that. – Ben Mar 14 '20 at 03:01
  • Welcome. `pizzaPrice` should take parameters, `size` and `numTop`, and you should pass in the values as arguments, i.e. `pizzaPrice(size, numTop)`. Though ideally you would change up the names to avoid [shadowing](https://en.wikipedia.org/wiki/Variable_shadowing). – wjandrea Mar 14 '20 at 03:04
  • 1
    You took away the rest of your choice so difficult to see. Where/how did you set the value of numTop? – ACCtionMan Mar 14 '20 at 03:05
  • Also to be clear, you still haven't provided a [mre] – wjandrea Mar 14 '20 at 03:07
  • Related: [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Mar 14 '20 at 03:15
  • Your while loop works for me - you might want to change the statement to `while size not in 'lms':` – wwii Mar 14 '20 at 03:21
  • If you dont want to call the function again then store its value and pass the value to the next function. – Shahir Ansari Mar 14 '20 at 05:19

1 Answers1

1

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. 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). In the pizzaPrice() function you need to specify that the size and numtop variables are the results of the numbofTops() and pizzaSize() functions.

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()
B-L
  • 144
  • 1
  • 8