用户输入错误后,我希望能够重新启动程序

时间:2019-10-12 00:50:10

标签: python-3.x

我正在使用python模块pypokedex编写一个程序,以编写一个询问口袋妖怪名称和生成方式的程序,并在其中展开一个https://www.serebii.net/页面。但是,如果用户拼写错误的神奇宝贝,它只会出错并关闭程序。我希望它重新启动程序。

我也有它,如果代码成功,它仍然会循环,问题出在何时失败(再次,这只是用户拼写错误的问题)

Asking the user for input until they give a valid response不起作用的原因是因为我需要输入特定的单词,而不是一定范围的数字。

import pypokedex
import webbrowser

while 1==1:

    name = input("What pokemon are you searching for? ")

    p = pypokedex.get(name = name)

    zero = 3-len(str(p.dex))

    if(zero == 2):
        num = "00" + str(p.dex)

    if(zero == 1):
        num = "0" +str(p.dex)

    if(zero == 0):
        num = str(p.dex)


    ngen = input("What generation number are you looking for? ")

    if(ngen == str(1)):
        gen = "pokedex"

    if(ngen == str(2)):
        gen ="pokedex-gs"

    if(ngen == str(3)):
        gen = "pokedex-rs"

    if(ngen == str(4)):
        gen = "pokedex-dp"

    if(ngen == str(5)):
        gen = "pokedex-bw"

    if(ngen == str(6)):
        gen = "pokedex-xy"

    if(ngen == str(7)):
        gen = "pokedex-sm"

    url = "https://www.serebii.net/" +  str(gen) + "/" + str(num) + ".shtml"\


    webbrowser.open(url , 2)

0 个答案:

没有答案