vendredi 24 juin 2016

Go back and retry when given wrong input & decision tree

So I'm brand new to coding and using python so I've been looking around at tutorials and now i'm trying to get my feet wet by just messing around a little. I do everything in ipython while making the script in the application Brackets on mac... dunno if that changes anything at all. Just some extra info.

So, i'm trying to make the decision input to where if one doesn't answer with "yes" or "no" then the code will respond "please answer with 'yes or 'no'" and then ask them again, over and over, until either "yes" or "no" is entered. I don't want the script to just end if something other than "yes" or "no" is entered.

print ("Welcome!")
myName = input("What is your name, friend?: ")
dec = input("So, " + (myName) + ", would you like to hear a story? (yes or no): ")
def decision():
    if (dec == "yes"):
        print("Wonderful")
    elif (dec == "no"):
        print("Maybe another time then?")
    else:
        print("Please answer with 'yes' or 'no'")
decision()

Also, how would I make a decision tree from this? So if someone answers "yes" then they will be asked another question and taken down a different rout within the story rather than if they had answered "no" (or something other than yes/no).

Thanks!

Aucun commentaire:

Enregistrer un commentaire