gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:57:04 25 4
gpt4 key购买 nike

我目前正在使用 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]

关于python - 如何让 Python 3.4 只使用一次 randrange 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27192415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com