We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

confused about while loop

im trying to make a simple calculator:

loop = 1
while loop == 1:
    print "Welcome to Jakes' Calculator"
    print "1) addition"
    print "3) multiplication"
    choice = raw_input("Enter a number 1-4: ")
    break
if choice.strip() == "1":
    a = input("enter the first number: ")
    b = input("enter the second number: ")
    c = a + b
    print a, "plus", b, "equals", c
elif choice.strip() == "3":
    a = input("enter the first number: ")
    b = input("enter the second number: ")
    c = a * b
    print a, "times", b, "equals", c
elif choice.strip() != "1" "3" :
    print "Not valid input"
loop = 1

the loop won't work, i want it to go back to the menu after it gives you an answer.

2
Contributors
2
Replies
13 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
pythoner
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You break out of loop without calculations and you have problem with syntax. If you use input in Python2 you can just input the calculation normally 3.234 + 23.12312 as it runs the expression through eval. It is not so safe though, so use raw_input allways:

while True:
    print "Welcome to Jakes' Calculator"
    print "1) addition"
    print "3) multiplication"
    print "0) quit"
    choice = raw_input("Enter the number: ").strip()
    if choice == "1":
        a = float(raw_input("enter the first number: "))
        b = float(raw_input("enter the second number: "))
        c = a + b
        print a, "plus", b, "equals", c
    elif choice == "3":
        a = float(raw_input("enter the first number: "))
        b = float(raw_input("enter the second number: "))
        c = a * b
        print a, "times", b, "equals", c
    elif choice != "0" :
        print "Not valid input"
    else:
         break
    print
pyTony
pyMod
Moderator
6,309 posts since Apr 2010
Reputation Points: 879
Solved Threads: 986
Skill Endorsements: 26

how silly of me to make a simple mistake. thanks

pythoner
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0669 seconds using 2.65MB