我只是python的初學者,而我在這裏只是寫了一個簡單的程序來自我評估自己,並嘗試以隨機順序回答問題。但是這裏的錯誤是randint函數有時會獲得已經獲得的相同數字,我會得到同樣的問題重複。我盡我所能來修復它,但我不能。我希望得到一些幫助here.`import隨機沒有重複的Python-randint函數
global some
global i
i=0
some=[]
names=['electric charge','permitivity of the medium','permitiovity of free space','relative permitivity of the medium','intensity of the electric field','electric potential at a point','electric potential energy','dipole moment','torque acting on the dipole','electric flux','linear charge density of the conductor','surface charge density of the conductor','capacitance of a parallel plate capacitor','practical unit of capacitance']
answer=['C','C2N-1m-2','C2N-1m-2','no unit','NC-1 or Vm-1','V','J','Cm','Nm','Nm2C-1','Cm-1','Cm-2','F','uF or pF']
def loop(i):
for i in range(i,len(names)):
global qno
qno=random.randint(0,len(names))
if i>0:
for k in range(0,len(some)):
if str(qno)==some[len(some)-1]:
loop(i-1)
print(names[qno])
print('Type your answer')
tell=input()
if tell==answer[qno]:
print('Right answer.Move on')
else:
print('Wrong answer,.The answer is '+answer[qno])
for j in range(i+1):
some.append(str(qno))
i=i+1
loop(i)
`
使用'random.shuffle(list)'並且你得到隨機順序的列表 - 然後你可以用'for'循環獲取元素。 – furas