Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
AJ Longstreet
Treehouse Project ReviewerHmm, Python while loop not working.
I was wondering if I could get a bit of advice. For some reason, my if statement is not working correctly. Regardless of what input I give it just calls the step9() function.
while True: print("Addition and Multiplication") print("(A)dd, (M)ultiply")
choice = input(" > ")
if choice is not "A" or "M":
step9()
elif choice is "A":
add()
elif choice is "M":
mult()
else:
break
Thanks, AJ
2 Answers
Steven Parker
243,318 PointsCleared .... are you sure?
I don't think that test will do what you think. I assume you want to test if the the input was anything other than an "A" or an "M". You could do it either of these ways, though:
if choice not in ("A", "M"): # with a tuple
if choice not in ["A", "M"]: # with a list
Then next, you are using the identity comparison ("is") where you want an equality comparison ("=="). The choice might have the same value (equality) as the literal string, but it will never be the same thing (identity).
AJ Longstreet
Treehouse Project ReviewerOK, I'll try that out as I try to get it working in Python 3. This works perfectly for me in Python 2.7. I'm going to see if I can optimize it and make it work in either version.
n1 = 0
n2 = 0
np = 0
choice = "string"
def add():
n1 = input("Please enter a number > ")
n2 = input("Please enter your second number > ")
np = n1 + n2
print("Sum is " + str(np))
def mult():
n1 = input("Please enter a number > ")
n2 = input("Please enter your second number > ")
np = n1 * n2
print("Product is " + str(np))
def step9():
print("Choose A or M")
while True:
print("Addition and Multiplication")
print("(A)dd, (M)ultiply or (D)ONE to exit")
choice = raw_input(" > ")
if choice in ("A" or "M"):
step9()
if choice is "A":
add()
if choice is "M":
mult()
if choice is "D":
break
print("Good Bye")
AJ Longstreet
Treehouse Project ReviewerAJ Longstreet
Treehouse Project ReviewerFirst hurdle cleared.
Changed to
Now to fix the rest.... lol