Python:当输入程序崩溃时输入非整数

时间:2017-01-12 13:44:30

标签: python python-2.7

我正在为python类项目的介绍制作一个程序。它是一个pokedex。我有一切设置,但我的朋友提出这样一个事实,当他输入任何非数字或超过151的数字将导致程序崩溃。我尝试使用elif语句来查看它是否isalpha()但是当我尝试它时,它会打印此错误。

Traceback (most recent call last):
  File "python", line 712, in <module>
  File "<string>", line 1, in <module>
NameError: name 'a' is not defined

所以我只需要帮助阻止它在任何输入无效时崩溃。完整的程序就在Link

#should define it here 
def print_userpoke_details(userpoke):
#take a parameter,the user input

    if userpoke == 1:
        print(Bulbasaur)
    elif userpoke.isalpha():
        print ("You did not enter a valid number")

while True:
    userpoke = input("Pokémon #:")
    #call the function here with user input
    print_userpoke_details(userpoke)

0 个答案:

没有答案
相关问题