0

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!

pierre700
  • 11
  • 6

2 Answers2

0

So if statements are going to be your best friend! Another thing I would recommend is while loops! So every time you ask the user to input a yes or no, do it in a while loop!

userAnswer =input("Yes or no??")
while (userAnswer != yes and userAnswer != no):
    userAnswer = input("Yes or no???")

basically keep on asking them until they say yes or no. It terms of your decision tree, you can do a LOT of nested if statements, or you can write a LOT of methods that you call after the user answers yes/no. I hope this helped!

Mr. DROP TABLE
  • 334
  • 2
  • 9
0

You can tidy up (and allow any case for input) Mr. DROP TABLEs answer like so:

userAnswer =input("Yes or no??")
while userAnswer.lower() in ('yes', 'no'):
    userAnswer = input("Yes or no???")
joel goldstick
  • 4,393
  • 6
  • 30
  • 46