我创建了一个列表,并且让用户能够选择他想要运行的方法。然而,当用户选择一个数字时,if else 语句不会被执行。有谁知道这是为什么?我是 IDLE 和 python 新手。
这是我的代码
aList = ['hello','bonjour','hola','ohayo','hello'];
print("our list is: ", aList)
print("1-count, 2-inList, 3-Reverse, 4-find, 5-insert")
choice = input("please call the function you want to execute: ")
if choice == 1:
word = input("which word would you like to count? ")
print(aList.count(word))
elif choice == 2:
inList = input("which word would you like to check if it's in the list")
if inList in aList:
print(True)
else :
print(False)
elif choice == 3:
for i in reversed(aList):
print(i)
elif choice == 4:
item = input("what item would you like to find?")
aList.index(item)
elif choice == 5:
item = input("what would you like to anwser? ")
aList.insert(item)
这是输出
our list is: ['hello', 'bonjour', 'hola', 'ohayo', 'hello'] 1-count, 2-inList, 3-Reverse, 4-find, 5-insert please call the function you want to execute: 1
block 引用> block 引用>>
总是在我输入号码后停止。我显然不太了解python
最佳答案
您需要转换为整数:
choice = int(input("please call the function you want to execute: "))
关于Python 空闲 : how to run the whole script as it is always stopping at some point,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28467041/