如何让python接受随机输入

时间:2013-12-23 11:57:45

标签: python python-2.7 input

我目前正在开发一个python项目,我需要它在某个时候接受用户的随机输入。

所以,如果我有,例如:

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

一切都有奇效。但是如何让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()

在这种情况下,我需要python接受任何输入,并仍然将用户带到下一个问题。我怎么能这样做?

3 个答案:

答案 0 :(得分:4)

然后删除if子句。

答案 1 :(得分:3)

如果输入不必是特定形式,或具有某些特定属性,则不需要if语句,甚至try

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

答案 2 :(得分:1)

如果您希望能够重新提出问题,如果他们没有做到正确,那么您可以像这样循环回来。这个问题唯一可接受的答案是“是”,或“是”等。 如果他们没有正确回答,他们会再次问他们,直到他们做对了。

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

如果你希望他们能够继续下一个问题,即使他们弄错了,你可以这样做:

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