[Tutor] Python Help - While Loops

David Lowry-Duda david at lowryduda.com
Wed Nov 13 14:30:03 EST 2019


> My code so far looks like this:
>
> print ("Welcome to The Donut Shop!")
> shopping = input ("To start shopping, press s. To leave the store, press
> q.")
> 
> while shopping == s:
>          name = (input("Please enter your name to begin: ")
>          print ("Please select a donut from the menu: ")
>          print ("1. Chocolate")
>          print ("2. Honey Crueller")
>          print ("3. Apple Fritter")
>          print ("4. Boston Cream")
>          donut = int(input ("Please select a number from 1 to 5: ")

You probably mean

while shopping == "s":  # note the quotes around the "s"
    name = (input("Please enter your name to begin: ")) # with second )

Aside from that, this code should run. But it probably doesn't quite do 
what you want. Within the while loop, the code will keep on running from 
the top of the loop to the bottom. Thus you will repeatedly ask for the 
user's name and repeately ask them to select a donut. You do not ever 
re-assign shopping. I might expect your code to be organized a bit more 
like this:

```python
# Only ask a name once
print("Welcome!")
name = input("Please enter your name: ")
shopping = input("Enter s to keep shopping: ")
while shopping == "s":
    # print donut descriptions
    donut = int(input("Which donut? "))
    # do something with donut
    shopping = input("Enter s to keep shopping: ")
```

This while loop only asks for the name once, and re-sets the `shopping` 
variable based on user input.

> under that I have a line to ask the quantity of what donut, and a  
> equation for the price, which calculates the total, but it only works 
> for one time. How would I properly create the while loop so that it 
> does not keep giving me errors in the while statement?

It's not clear what other problems you're having or how to fix them, 
since we don't have the code. The code you originally posted would not 
have run once (due to the missing quotes around "s" and the missing 
right parenthesis). If you have code whose behavior you want to debug 
with the list, you should post that code.

Good luck - DLD

-- 
David Lowry-Duda <david at lowryduda.com> <davidlowryduda.com>


More information about the Tutor mailing list