-4

My code surrounding the while loop is, for the most part, working fine. However the while loop won't stop repeating even if the user inputs 5 which should make it exit. This now includes all code relevant to the function I am trying to debug, I apologize for any confusion:

def dinos():
welcomeTheCustomer()
selection = "\0"
while selection != "4":
    selection = requestUserInput()

    if selection is "1":
        order()
    elif selection is "2":
        checkOut()
    elif selection is "3":
        startOver()

print("Have a good day!")


def askUserToChooseADonut():
print("\n -MENU-\n 1.Strawberry Twizzler\n 2.Chocolate-dipped Maple Puff\n    3.Vanilla Chai Strudel\n 4.Honey-Drizzled Lemon Dutchie\n 5.Done\n")
donutChoice = int(raw_input("Please enter a selection: "))
return(donutChoice)

def askUserToSpecifyDonutQuantity():
donutQuantity = "d"
while not donutQuantity.isdigit():
    donutQuantity = str(raw_input("Please enter a quantity of donuts: "))
return int(donutQuantity)
print ("Confirmed")

def order():
donutChoice = 0

donutQuantity1 = 0
donutQuantity2 = 0
donutQuantity3 = 0
donutQuantity4 = 0

while not (donutChoice == 5): # loop until the customer selects '5. Done'
    donutChoice = int(askUserToChooseADonut())
    donutQuantity = askUserToSpecifyDonutQuantity()

    if donutChoice is 1:
        donutQuantity1 = donutQuantity
    elif donutChoice is 2:
        donutQuantity2 = donutQuantity
    elif donutChoice is 3:
        donutQuantity3 = donutQuantity
    elif donutChoice is 4:
        donutQuantity4 = donutQuantity

return (donutQuantity1, donutQuantity2, donutQuantity3, donutQuantity4)
A.Lostman
  • 19
  • 2

5 Answers5

2

you are testing donutChoice to a string in this line

while donutChoice != "5":

but with an int in this one

if donutChoice is 1:

I assume the variable donutChoice is an int so your while loop should be like this

while donutChoice != 5

(Btw, I think you should use '==' instead of 'is').

Community
  • 1
  • 1
huapito
  • 395
  • 2
  • 10
  • It is an integer but even when changing "5" to 5 the while loop will not stop repeating. I need it to loop as long as donutChoice is not equal to 5 so I don't understand how using == would help? – A.Lostman Oct 05 '15 at 19:23
  • I recommend you to add some log lines and print out `type(donutChoice)`, `donutChoice==5` and `donutChoice=="5"`, for an easier debug – huapito Oct 05 '15 at 19:26
  • I added donutChoice==5 to the code but I don't understand what you mean by log lines? Sorry I am very new to coding. – A.Lostman Oct 05 '15 at 19:32
  • By "log lines" I meant that you "log" (by printing) what you are doing. After the `while` line, add a line like this: `print "donutChoice: '%s', type:'%s', comp1:'%s', comp2:'%s'" % (donutChoice, type(donutChoice), donutChoice==5, donutChoice=="5")` and tell us what you see. That line should print some useful information every time you enter the loop: the value of donutChoice and its type, and the value of its comparison (both with `5` and `"5"` – huapito Oct 05 '15 at 19:38
0

I'm pretty sure you're missing a main concept in Python: 7 and "7" are NOT the same.

>>> a = 7
>>> b= "7"

>>> print(type(a))
<class 'int'>

>>> print(type(b))
<class 'str'>

Having while != 5 will continue looping for [-inf-4] U [6-inf]. But if you change to while <5 will loop only for [-inf-4].

Leb
  • 15,483
  • 10
  • 56
  • 75
0

You are mixing strings and integers. Convert everything to int. Ideally, the functions should all return integers.

Also, it appears that you do not have to use all those similar variables. Instead, use a list:

donutChoice = 0
donutQuantity = [0] * N_DONUT

while donutChoice in range(5):
    donutChoice = int(askUserToChooseADonut())
    quantity = int(askUserToSpecifyDonutQuantity())
    # There should be verification in "askUserToChooseADonut",
    # so this if should not be necessary.
    if donutChoice != 0
        donutQuantity[donutChoice-1] += quantity
return donutQuantity
Thierrypin
  • 21
  • 1
  • 4
0

With this code, you're accepting input as a string, and comparing it to an integer.

while donutChoice != 5: # loop until the customer selects '5. Done'
    donutChoice = askUserToChooseADonut()

Change this to force the input to be an integer:

while donutChoice != 5: # loop until the customer selects '5. Done'
    donutChoice = int(askUserToChooseADonut())

(You should do the same for your donut quantity if this is expected to be an integer)

From the console, you can see why donutChoice will never equal 5.

>>> "1" == 1
False

Furthermore, you should change all your if donutChoice is ... to if donutChoice == .... It will work for low integers, because python interns numbers between 1 and 256 for better performance (so is will work), it will start to behave weirdly if these numbers grow. It's recommended to always use == for integer comparisons.

>>> a = 1
>>> a is 1
True
>>> a = 257
>>> a is 257
False
Rejected
  • 4,445
  • 2
  • 25
  • 42
0

This Code is working fine for me. I'm using python 3.3.3

def order():

donutChoice = "0"

donutQuantity1 = 0
donutQuantity2 = 0
donutQuantity3 = 0
donutQuantity4 = 0

while donutChoice != "5": # loop until the customer selects '5. Done'
    donutChoice = input("Enter a choice: ")
    donutQuantity = input("Enter the Quantity: ")

    if donutChoice is 1:
        donutQuantity1 = donutQuantity
    elif donutChoice is 2:
        donutQuantity2 = donutQuantity
    elif donutChoice is 3:
        donutQuantity3 = donutQuantity
    elif donutChoice is 4:
        donutQuantity4 = donutQuantity

return (donutQuantity1, donutQuantity2, donutQuantity3, donutQuantity4)

order()

TUSHAR

Tushar Niras
  • 3,654
  • 2
  • 22
  • 24