0

I am writing the code for a calculator using python.

loop = 1
while loop == 1:    #loop calculator while loop is TRUE
    print   ("""Options:
        Addition       1)
        Subtraction    2)
        Multiplication 3)
        Division       4)
        Quit           5)   
        Reset Sum      6) """)      #display calculator's options
    (' ')   #spacer
    choice = input("option: ")  #get user input for calculator options
#----------------------------------------------------------------------------Addition
    if choice == 1:     #check if user wants to add
        print (' ') #spacer
        print ('Addition Loaded!')  #tell the user they are adding
        print (' ') #spacer
        add1 = int(input('Base Number'))    #get value for add1
        sum = int(input('Number ta add'))   #get value for sum
        sum = add1 + sum    #make sum equal the sum of add1 and sum
        print (str(sum))    #print sum
        addloop = 1     #set addloop to TRUE
        while addloop == 1:     #continue addition while addloop = TRUE
            add1 = int(input('Additional number to add'))   #get value for add1
            if add1 == 0:   #check if add1 equals zero
                print (' ') #spacer
            sum = add1 + sum    #make sum equal the sum of add1 and sum
            if add1 != 0:   #check if add1 is not equal to 0
                print (str(sum))    #print sum
            if add1 == 0:   #check if add1 is equal to 0
                print ('Total: ',)  #print prefix
                print (str(sum))    #print sum
                addloop = 0 #set addloop to FALSE

Below the addition section listed here, there is other sections for subtraction, multiplication, etc that use ELIF instead of IF (as they should be?). The problem is, when the user picks an option for the calculator (addition, subtraction...), it instead loops back to the while loop, without it going through any of the IF or ELIF statements. Do IF statements not work inside a WHILE loop?

kaya3
  • 47,440
  • 4
  • 68
  • 97
stapler8
  • 15
  • 1
  • 1
  • 2
  • 2
    There's no need to comment every line in the source code, especially if the comment just repeats the code – Jasper Jan 28 '15 at 19:32
  • This problem can be difficult to figure out... `print(choice)` seems to show an integer... but `print(repr(choice))` shows its really a string (which is never equal to an integer). – tdelaney Jan 28 '15 at 19:34
  • @Jasper I commented every line to get into the habit of doing comments at all. I don't intend on commenting unnecessary lines in the future, it's just to help me learn to do it early on. – stapler8 Jan 28 '15 at 20:02

1 Answers1

4

Here's your problem:

choice = input("option: ")

Under Python 3, this puts a string into choice. You need an integer:

choice = int(input("option: "))

This will raise a ValueError if the user types something which is not an integer. You can catch that with a try/except ValueError block, or you can leave the choice line as it appears in the question and change all your comparisons to look like this:

if choice == "1":  # compare to a string instead of an integer
Kevin
  • 28,963
  • 9
  • 62
  • 81