我正在尝试制作一个“谁想成为百万富翁”风格的程序,其中包含随机问题、随机位置的随机答案(在 Python 中)。我正在努力形成一个重复自身的while循环,以便两个变量永远不会相同。我的循环(显然不起作用)是:
while rnd4 == rnd2:
rnd4 = random.randint(1,3)
break
在我当前的程序中:
if rnd2 == 1:
position1 = answer #the random question has already been printed and the
#answer set
elif rnd2 == 2:
position2 = answer
elif rnd2 == 3:
position3 = answer
rnd3 = random.randint(1,4)
rnd4 = random.randint(1,3)
while rnd3 == rnd1:
rnd3 = random.randint(1,4) #these loops are meant to mean that the same
break #answer is never repeated twice + the same
while rnd4 == rnd2: #position is never overwritten
rnd4 = random.randint(1,3)
break
if rnd3 == 1:
if rnd4 == 1:
position1 = (no1[1])
elif rnd4 == 2:
position2 = (no1[1])
elif rnd4 == 3:
position3 = (no1[1])
...(跳过代码)
print (position1)
print (position2)
print (position3)
由于程序的随机性,它很难测试,但我期待这样的格式:
q1
answer
() #only two of the positions have had code written for them yet
random answer
或以上的组合。(基本上一个正确答案和一个错误答案在不同的位置)偶尔我得到:
q1
answer
answer
()
或者
q1
random answer
()
()
所以这显然是错误的,有时相同的答案会打印两次,或者错误的答案会覆盖正确的答案。
为这么一个冗长的问题道歉,答案可能很简单(如何修复循环),但我知道我不应该要求代码!
非常感谢!