-4
Question1 = input("We will start off simple, what is your name?")
if len(Question1) > 0 and Question1.isalpha(): #checks if the user input contains characters and is in the alphabet, not numbers.
    Question2 =  input("Ah! Lovely name, %s. Not surprised you get all the women, or is it men?" % Question1)
    if Question2.lower() in m: #checks if the user input is in the variable m
        print ("So, your name is %s and you enjoy the pleasure of %s! I bet you didnt see that coming." % (Question1, Question2))
    elif Question2.lower() in w: # checks if the user input is in the variable w
        print ("So, your name is %s and you enjoy the pleasure of %s! I bet you didnt see that coming." % (Question1, Question2))
    else: #if neither of the statements are true (incorrect answer)
        print ("Come on! You're helpless. I asked you a simple question with 2 very destinctive answers. Restart!")
else:
    print ("Come on, enter your accurate information before proceeding! Restart me!") #if the first question is entered wrong (not a name)
Question3 = input("Now I know your name and what gender attracts you. One more question and I will know everything about you... Shall we continue?")

In order to get the right answer, I will first tell you what happens when I run it: There are several scenarios but I will walk you through 2. When I run the program I am first asked what my name is, I can enter anything here that is alphabetic, then it asks me if I like men or woman, weird I know but it's a project I am working on, if I were to say 'men' or 'women' the program runs perfectly, but if I were to enter say... 'dogs' it would follow the else statement print ("Come on! You're helpless. I asked you a simple question with 2 very destinctive answers. Restart!") but then it continues the code and goes to the last line shown above "Now I know your name and what gender attract you...... blah blah". What I am trying to do is have it restart the script if you were to enter anything other than 'men' or 'women'. I was told that a while statement would work, but I would need an explanation as to why and how... I'm new to Python in a sense.

Gu--
  • 5
  • 1
  • 7

2 Answers2

0

That's what loops are for.

while True:
    # your input and all that here
    if answer == "man" or answer == "woman":
        # do what you want if the answer is correct
        break # this is important, as it breaks the infinite `while True` loop
    else:
        # do whatever you want to do here :)
        continue
iScrE4m
  • 882
  • 1
  • 12
  • 31
0

If you want the script to repeat indefinitely, you can simply wrap everything in a while loop like so:

while True:
    # Do yo your logic here.
    # Break out when a condition is met.

If you want to be more savvy with it you can use a method and have the method call itself over and over until it's done.

def ask_questions():
    # Do your logic here
    if INPUT_NOT_VALID:
        ask_questions()
    else:
        # Continue on.

Also, search on this site for you text because I've seen about five or six questions all involving this programming scenario which means it has to be a common problem from a programming class (one I'm assuming you are in). If that's the case discuss with classmates and try to stimulate each other to find the solution instead of just posting online for an answer.

Michael Platt
  • 1,297
  • 12
  • 25
  • I really appreciate the variety of answers, I will look into the both of them very soon! Unfortunately, I am a high school student who does programming for fun, I learn alone and have no online classes I do other than codeacademy haha Thank you very much, I will make sure to search the site for related questions. thanks – Gu-- Aug 26 '16 at 20:34