如何让Python 3.4只使用一次randrange变量?

时间:2014-11-28 16:07:48

标签: python random

我目前正在使用Python 3.4&randrange函数构建一个小测验游戏。我有它设置并努力自动选择一个变量用于选择一个问题。我唯一的问题是,每次参加考试时,我怎样才能让它只使用一次这个问题。

#Main Game
def game():
    score = 0
    while True:
        irand = randrange(0,4)
        #Question 1
        if irand == 1:
            quiz += 1
            print ("What was the first Pokémon?")
            time.sleep(0.5)
            print ("[A] Bulbasaur")
            time.sleep(0.5)
            print ("[B] Pikachu")
            time.sleep(0.5)
            print ("[C] Nicki Minaj")
            time.sleep(0.5)
            print ("[D] Arceus")
            time.sleep(1)
            question1 = input ("Your Answer: ")
            if question1 == 'a':
                score += 1
                print ("Your answer is correct!")
                time.sleep(2)
                print ("Your score is now %s" % score + "!")
                time.sleep(4)
                cls()
                continue
            if question1 == 'b' or 'c' or 'd':
                print ("Your answer is incorrect!")
                time.sleep(4)
                cls()
                continue
        #Question 2
        if irand == 2:
            quiz += 1
            print ("What item shares experience with your party of Pokémon?")
            time.sleep(0.5)
            print ("[A] Exp Distributor")
            time.sleep(0.5)
            print ("[B] Exp Party")
            time.sleep(0.5)
            print ("[C] Exp Share")
            time.sleep(0.5)
            print ("[D] Exp Gain")
            time.sleep(1)
            question2 = input ("Your Answer: ")
            if question2 == 'c':
                score += 1
                print ("Your answer is correct!")
                time.sleep(2)
                print ("Your score is now %s" % score + "!")
                time.sleep(4)
                cls()
                continue
            if question2 == 'a' or 'b' or 'd':
                print ("Your answer is incorrect!")
                time.sleep(4)
                cls()
                continue
        #Question 3
        if irand == 3:
            quiz += 1
            print ("What Pokémon is the god Pokémon?")
            time.sleep(0.5)
            print ("[A] Arceus")
            time.sleep(0.5)
            print ("[B] Magikarp")
            time.sleep(0.5)
            print ("[C] Meowth")
            time.sleep(0.5)
            print ("[D] Pikachu")
            time.sleep(1)
            question3 = input ("Your Answer: ")
            if question3 == 'a':
                score += 1
                print ("Your answer is correct!")
                time.sleep(2)
                print ("Your score is now %s" % score + "!")
                time.sleep(4)
                cls()
                continue 
            if question3 == 'b' or 'c' or 'd':
                print ("Your answer is incorrect!")
                time.sleep(4)
                cls()
                continue
        #Quiz Ending
        if quiz == 20:
            print ("The quiz is now over.")
            time.sleep(2)

我还没有尝试过任何东西,因为我还在学习Python。非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

你可以做一些类似于现实生活的事情:你手中有一套牌,随机洗牌,拿第一张,丢弃它,从牌组拿另一张等等。

在Python中,你可以通过使用问题列表,随机改组问题,然后逐个采集每个问题来做到这一点。

示例:

>>> import random
>>> cards = [1,2,3,4,5]
>>> random.shuffle(cards)
>>> cards
[3, 5, 2, 1, 4]

答案 1 :(得分:0)

不是每次都选择一个随机数,而是尝试改组数字列表以随机化问题顺序:

from random import shuffle

# range creates a list: [1, 2, 3]
questions = range(1, 4)
shuffle(questions)

for q in questions:
    if q == 1:
        #question 1
    if q == 2:
        #question 2

为了不断改进您的程序,请考虑使用字典来存储您的问题,并编写一个函数来处理问题和答案部分。这样,您只需编写一次问题代码,就可以反复使用新问题。