Skip to main content
 首页 » 编程设计

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

2025年05月04日13JeffreyZhao

我目前正在使用 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。非常感谢任何帮助。

请您参考如下方法:

你可以做一些与现实生活中类似的事情:你手里拿着一组牌,随机洗牌,拿第一张,弃掉它,从牌堆中拿另一张,等等。

在 Python 中,您可以通过使用问题列表、随机打乱问题顺序,然后一次回答每个问题来做到这一点。

例子:

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