1

I'm still quite new to Python and am confused as to what's happening with a program I'm working on. The code is below. The problem is the code is never running any of the if/elif/else lines. It just keeps cycling the menu and inputs. I'm using 3.2.

# Program to Add, Subtract, Multiply, and Divide

def printmenu():
    print("Calculator v0.01")
    print("[A]dd Two Numbers")
    print("[S]ubtract Two Numbers")
    print("[M]ultiply Two Numbers")
    print("[D]ivide Two Numbers")
    print("[Q]uit the Program")

choice = "x"

while choice.lower != "q":
    printmenu()
    choice = input("What would you like to do? ")
    firstnum = input("What is the first number? ")
    secnum = input("What is the second number? ")
    if choice.lower == "a":
        print("The answer is ",  (firstnum + secnum))
    elif choice.lower == "s":
        print("The answer is ",  (firstnum - secnum))
    elif choice.lower == "m":
        print("The answer is ",  (firstnum * secnum))
    elif choice.lower == "d":
        print("The answer is ",  (firstnum / secnum))
    else:
        print("Choice not recognized.  Try again!")

P.S. - This is my first post on here, so if I'm not doing something properly then please let me know.

Thanks!

JT

1
  • Your post is fine - your question title is indicative of the content of the post/what you want answered, you supply the code that is not working, you say what is not working and what you expect to happen instead. Commented Apr 20, 2013 at 11:24

3 Answers 3

3
>>> "a".lower
<built-in method lower of str object at 0x0000000001EBBBC0>
>>> "a".lower()
'a'
>>> "a".lower == "a"
False
>>> "a".lower() == "a"
True
>>>

I think you meant lower(), not lower ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that was it! Of course now it's not acting correctly because I didn't convert the nums to float/int so 5 +2 = 52, but that's easy enough to fix by putting float(input()). Thanks for your help.
0

all inbuilt functions in python should be called using (). So use

if choice.lower()!="q":

Only then the function is called. Else the value is always returned False, since no function is executed

Comments

0
  1. Replace lower by lower()
  2. Use float(fisrtnum) + float(secnum) as otherwise it will just concatenate the char and gives 23 for '2'+'3'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.