0

I am making a program using the python module pypokedex, to make a program that will ask for a pokemon's name, and the generation, and pull up a https://www.serebii.net/ page about it. But, if the user spells a pokemon incorrectly, it just errors and shuts down the program. I want it to restart the program.

I also have it so that if the code succeeded, it will still loop, the problem is when it fails, (again, this is only a problem if the user spells a name wrong)

the reason this:Asking the user for input until they give a valid response doesn't work is because I need to have it be certain words, not a range of numbers.

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)
  • 1
    It'd be a lot easier to use a `try` and just prevent the program from closing in the first place. – Carcigenicate Oct 12 '19 at 00:52
  • im kind of new to python, how would i do that? – DTWaffleKing Oct 12 '19 at 00:59
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wjandrea Oct 12 '19 at 01:02
  • @DTWaffleKing Learn about `try` and `except` and it will make sense. Those are very important aspects of the language. – Carcigenicate Oct 12 '19 at 01:03
  • I see your edit. If you read the [accepted answer](https://stackoverflow.com/a/23294659/4518341) on that question, you'll find your solution. The question itself is more of an example situation. Otherwise, you need to make a [mre] of the problem you're experiencing. – wjandrea Oct 12 '19 at 01:08

0 Answers0