简体   繁体   中英

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?

只需删除if子句即可。

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()

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM