Dr.Garfeel Ph.D
Dr.Garfeel Ph.D

Reputation: 33

How to make python accept random input

I'm currently working on a python project and I need it to accept random input from users at some point.

So, if I have, for example:

def question_one () :
    answer_one = raw_input ('How many days are there in a week? ').lower()
    try: 
        if answer_one == 'seven' or answer_one == '7' :
            question_2()

Everything works wonders. But how can I make python accept random input, as in

def question_two () :
    answer_two = raw_input ('What´s your mother´s name? ').lower()
    try: 
        if answer_two == ***I have no idea how to code this part*** :
            question_3()

In this case, I would need python to accept any input and still take the user to the next question. How could I do it?

Upvotes: 0

Views: 251

Answers (3)

CRABOLO
CRABOLO

Reputation: 8793

If you want to be able to re-ask the question, if they don't get it right then you can loop back to it like so. The only acceptable answer for this question is "yes", or "YES", etc.. If they don't answer correctly it will ask them again,till they get it right.

def question1():
    answer1 = raw_input("Do you like chickens?")
    answer1 = answer1.lower()
    if answer1 == 'yes':
        print "That is Correct!"
        question2()
    else:
        question1()

If you want them to be able to go on to the next question even if they get it wrong, you can do like so:

def question1():
    answer1 = raw_input("Do you like chickens?")
    answer1 = answer1.lower()
    if answer1 == 'yes':
        print "That is Correct!"
    else:
        print "Next question coming!"
    question2()

def question2():
    answer2 = raw_input("How many days in a week?")
    answer2 = answer2.lower()
    if answer2 == '7' or answer2 == "seven":
        print "That is Correct!"
    else:
        print "Sorry,that was wrong"
    question3()

Upvotes: 1

bruno desthuilliers
bruno desthuilliers

Reputation: 77892

Just remove the if clause then.

Upvotes: 4

henrebotha
henrebotha

Reputation: 1298

If the input doesn't have to be of a specific form, or have some particular property, then there's no need for the if statement, or even the try.

def question_two():
    answer_two = raw_input("What's your mother's name?").lower()
    question_3()

Upvotes: 3

Related Questions