简单的elif声明

时间:2017-07-21 13:09:04

标签: python if-statement

当我运行此代码时,它总是转到else。即使我选择1,2或3,我怎样才能获得正确的if, elif else声明?

def main():
    name = input("What is your name?")
    print("Hello", name)
    choice = int
    choice = 0
    choice = input("Please choose a number from the following options: \n 1. Paper \n 2. Rock \n 3. Scissors")
    print (choice)

    if choice == 1:
        print ('You chose number 1')
    elif choice == 2:
        print ('You chose number 2')
    elif choice == 3:
        print ('You chose number 3')
    else:
        print ('Invalid Entry Mush')
    return

main()

2 个答案:

答案 0 :(得分:1)

input返回一个字符串。您需要将其强制转换为int,以便触发检查整数的elif选项:

choice = int(input("Please choose a number from the following options: \n 1. Paper \n 2. Rock \n 3. Scissors"))

顺便说一下,在那之前的两行是不必要的。

答案 1 :(得分:0)

或者您可以将if语句放在引号之间:ex:if choice ==" 1"打印(" nr 1")elif choice ==" 2"等...... ..

相关问题