mercredi 28 janvier 2015

Why doesn't IF seem to work inside a WHILE loop? (Python 3.4)

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?


Aucun commentaire:

Enregistrer un commentaire