“或”条件导致“如果”的问题

时间:2013-01-02 00:57:50

标签: python if-statement

我在函数中遇到or条件时遇到问题。无论if是什么值,True语句都会评估为choice。当我删除or时,if正常工作。

def chooseDim ():
    **choice = input ('Do you need to find radius or area? ')
    if choice == 'A' or 'a':**
        area = 0
        area = int(area)
        areaSol ()

    elif choice == 'R' or 'r':

        radSol ()

    else:
        print ('Please enter either A/a or R/r.')
        chooseDim ()

4 个答案:

答案 0 :(得分:5)

关于or本身的答案是正确的。你真的在问"a"是否为真,它总是如此。但是还有另一种方法:

if choice in 'Aa':

然后再说,没有错:

if choice.lower() == 'a':

答案 1 :(得分:4)

'a'的计算结果为True,因此您需要正确构造if语句。

def chooseDim ( ):
**choice = input ('Do you need to find radius or area? ')
if choice == 'A' or choice == 'a':**
    area = 0
    area = int(area)
    areaSol ( )

elif choice == 'R' or choice == 'r':

    radSol ( )

else:
    print ('Please enter either A/a or R/r.')
    chooseDim ( )

答案 2 :(得分:2)

在这种情况下,使用in运算符会更容易:

if choice in ['A', 'a']: ...

答案 3 :(得分:0)

if choice == 'A' or choice =='a':
相关问题