如何改变Mad Libs游戏中的数字?

时间:2016-02-17 14:25:47

标签: python

我试图创建一个疯狂的Libs风格的游戏,其中有1分钟的时间限制。一旦达到时间限制,游戏就结束了,并且游戏结束了" GAME OVER"被展示。如果用户在时间限制内完成,则显示结果。我使用while循环和time.time()以便第一个问题被问到,while循环重复,第二个问题被问到,while循环重复,等等。但我无法弄清楚如何在游戏进行时更改问题编号,以便当while循环完成一个问题时,它会移动到下一个问题。

start = time.time()
    questionNum = 1

    question1 = "Enter the name of a male acquaintance: "
    question2 = "Enter the name of a place: "
    question3 = "Enter an adverb: "
    question4 = "Enter a verb: "
    question5 = "Enter a plural noun: "
    question6 = "Enter a verb: "
    question7 = "Enter the vocation of a person: "
    question8 = "Enter a verb ending with -ing: "
    question9 = "Enter an emotion (past tense): "
    question10 = "Enter a grade letter: "
    question11 = "Enter a school subject: "
    question12 = "Enter the meaning of the letter grade A: "
    question13 = "Enter the meaning of the letter grade B: "
    question14 = "Enter the meaning of the letter grade C: "
    question15 = "Enter the meaning of the letter grade D: "
    question16 = "Enter the meaning of the letter grade E: "
    question17 = "Enter the meaning of the letter grade F: "
    question18 = "Enter an aggressive verb: "

    while time.time() - start < 60:
        answer1 = input(question1)
        questionNum +=1
    else:
        print("Too bad, slowpoke. GAME OVER!")

1 个答案:

答案 0 :(得分:0)

您可能希望将它们放在某种列表中:

questions = [ "Enter the name of a male acquaintance: ",
  "Enter the name of a place: ",
  ... ]

然后你也会在列表中收集答案:

answers = []
while time.time() - start < 60:
    answers.append(input(questions[questionNum]))
    questionNum +=1

然后,您可以通过比较问题和答案列表的长度来检查用户是否已回答所有问题:

if len(answers) < len(questions):
    print("Too bad, slowpoke. GAME OVER!")