mardi 23 février 2016

Python nested IFs and whiles [duplicate]

This question already has an answer here:

I'm just getting into python and figuring it out through my js knowledge. I do seem to run into some syntax problems and haven't been able to figure this one out.

I am trying to make the code ask the user a series of questions. Each question has a series of answers that can be given and if the user's answer isn't equal to one of those options it should re-ask the question.

I tried this without using loops and just used the else for each one but that made it re-ask the question only once if the wrong answer was given. Now with the while loops it just moves to the next question even if a correct answer is not given.

user = input("What is your username?")
race = input("What is your race? (orc, human, goblin, elf, gnome, troll)")
if race == "orc" or "human" or " goblin" or "elf" or "gnome" or "troll":
    pClass = input("What is your class? (rogue, archer, warrior, cleric, warlock, mage, paladin)")
    if pClass == "rogue" or "archer" or "warrior" or "cleric" or "warlock" or "mage" or "paladin":
        correct = input("So you are " + user + ", a " + race + " " + pClass + "? (yes or no)")
    else:
        pClass = input("What is your class? (rogue, archer, warrior, cleric, warlock, mage, paladin)")
        while pClass != "rogue" or "archer" or "warrior" or "cleric" or "warlock" or "mage" or "paladin":
            pClass = input("What is your class? (rogue, archer, warrior, cleric, warlock, mage, paladin)")
else:
    race = input("What is your race? (orc, human, goblin, elf, gnome, troll)")
    while race != "orc" or "human" or " goblin" or "elf" or "gnome" or "troll":
        race = input("What is your race? (orc, human, goblin, elf, gnome, troll)")

Aucun commentaire:

Enregistrer un commentaire