简体   繁体   English

如何使python接受随机输入

[英]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. 我目前正在处理一个python项目,我需要它在某个时候接受用户的随机输入。

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 但是如何使python接受随机输入,如

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. 在这种情况下,我需要python接受任何输入,仍然将用户带到下一个问题。 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 . 如果输入不必具有特定的形式或具有某些特定的属性,则不需要if语句,甚至不需要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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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