一遍又一遍地要求raw_input

时间:2016-03-04 03:23:30

标签: python

我遇到问题让我的代码只打印我的问题。我能够让它打印问题和答案,但现在进一步。我确信在骑行中还有更多问题,但这是我现在陷入困境的地方。

#gives the blank spaces in the questions to the quiz
num_of_blank = ["blank1", "blank2", "blank3", "blank4"] 

#Below are the levels of questions for the quiz
easy_question = """A blank1 is something that holds a blank2, and a blank3 is a list of characters in order.  
You can also blank4 a string to a variable."""

medium_question = """An example of blank1 is an blank2 statement.  If statements can run at most blank3 time.  
Unlike the if statement, a blank4 loop can run any number of times."""

hard_question = """A blank1 is when you have a list within a list.  Lists support blank2, this means we can change the value of a list 
after it has been created.  When you have two different ways to refer to the same object that is called blank3.  In a blank4 loop, for 
each element in the list, you will assign that element to the name and evaluate the block."""

#the answers to each quiz
easy_answers = ["variable", "value", "string", "assign"]
medium_answers = ["control flow", "if", "one", "while"]
hard_answers = ["nested", "mutation", "aliasing", "for"]

#determines the level of difficulty for the questions for the user
def question_level(difficulty):
    difficulty = raw_input("Please select a level of difficulty for your questions: Easy, Medium, or Hard. ").lower()
    question, answers = question_level()
    if difficulty == "easy":
        return (easy_questions, easy_answers)
    elif difficulty == "medium":
        return (medium_question, medium_answers)
    elif difficulty == "hard":
        return (hard_question, hard_answers)
    else:
        print "That is not a level."
        question_level()
print question_level()

2 个答案:

答案 0 :(得分:0)

您正在函数内调用函数,这是一个递归调用,在这种情况下您不需要。尝试检查函数之外的错误,例如:

def question_level():
    difficulty = raw_input("Please select a level of difficulty for your questions: Easy, Medium, or Hard. ").lower()
    if difficulty == "easy":
        return (easy_question, easy_answers)
    elif difficulty == "medium":
        return (medium_question, medium_answers)
    elif difficulty == "hard":
        return (hard_question, hard_answers)
    else:
        print "That is not a level."
        return None

q = None
while q is None:
    q = question_level()
    print(q)

答案 1 :(得分:0)

你在这一行上有一个递归调用

question, answers = question_level()

只需删除question_level()部分并初始化其他内容即可。