简体   繁体   English

为什么IF在WHILE循环中似乎不起作用? (Python 3.4)

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

I am writing the code for a calculator using python. 我正在使用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?). 在此处列出的加法部分下面,还有其他一些用于减法,乘法等的部分,这些部分使用ELIF代替IF(应该是吗?)。 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. 问题是,当用户为计算器选择一个选项(加法,减法...)时,它将循环返回while循环,而无需通过任何IF或ELIF语句。 Do IF statements not work inside a WHILE loop? IF语句在WHILE循环中不起作用吗?

Here's your problem: 这是您的问题:

choice = input("option: ")

Under Python 3, this puts a string into choice . 在Python 3下,这会将字符串放入choice You need an integer: 您需要一个整数:

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

This will raise a ValueError if the user types something which is not an integer. 如果用户键入的不是整数,则将引发ValueError 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: 您可以使用try / except ValueErrorexcept ValueError ,也可以保留问题中显示的choice行,并将所有比较更改为如下所示:

if choice == "1":  # compare to a string instead of an integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM