2017-01-31 64 views
0

我只是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) 
` 
+0

使用'random.shuffle(list)'並且你得到隨機順序的列表 - 然後你可以用'for'循環獲取元素。 – furas

回答

1

你的函數之前,添加描述boolean數組哪些問題已經回答了:

already_asked = [False] * len(names) 

那麼,在您指定俠諾到值時,應生成隨機數,直到你打一個你有沒有問過,並標註新問的一個問題是問:

qno = random.randint(0, len(names)) 
while already_asked[qno]: 
    qno = random.randint(0, len(names)) 
already_asked[qno] = True 
+0

這是正確的答案。 Deepak,你正在禁止'randint()'函數給你一個它以前給你的值。然而,爲了做到這一點,你需要跟蹤返回的值,以便不再返回它們。 – mmenschig

+0

非常感謝@ AdamVenis。雖然我必須花一些時間來理解背後的邏輯,但這是我一直期待的。您回答了我的第一個問題....! <3 <3 –

0

如果你使用randomint(0,14),你一定會得到很多重複! 例如:

import random 

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'] 


for i in range(10): 
...  print(random.randint(0,len(names))) 

這裏是我的輸出,在我第一次運行:

12 
6 
6 
6 
7 
14 
7 
11 
4 
10 

注意我是如何得到了數6三倍!顯然重複次數會隨着範圍的增加而減少,但是你總是會有重複數字的機會,尤其是因爲這只是一個僞隨機生成的數字。

也許你正在尋找像洗牌一樣的東西?例如:

>>> new_order = ([ i for i in range(len(names)) ]) 
>>> random.shuffle(new_order) 
>>> print(new_order) 
[9, 10, 7, 8, 4, 13, 0, 2, 3, 6, 12, 5, 1, 11] 
0

也許有點先進的,它很高興有不需要明確的索引選項

import random 

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'] 

# zip, list comprehensions are useful things to learn 
# this helps keep things together without explicit index calcs 
name_answer_pairs = [(n, a) for n, a in zip(names, answer)] 

atest = name_answer_pairs[:] # need a copy, atest gets modified below 
random.shuffle(atest) 

yes = False 

while atest:  # loops until atest is empty, add your UI code to loop 
    quest, ansr = atest.pop() # gets the pair, removes the tuple from the end 
    print('Q: ',quest, '\n', 'Ans: ', ansr) # just to show an example 

    # do your user Q/A thing here 

    # check if atest is empty, to repeat loop with new atest, refresh atest: 
    if not atest: 
    # ask user to continue?, set 'yes' True or False 
     if yes:  
      atest = name_answer_pairs[:]  
      random.shuffle(atest)   # new atest, different order 
      continue 
     else: 
      break 

Q: practical unit of capacitance 
Ans: uF or pF 
Q: electric charge 
Ans: C 
Q: capacitance of a parallel plate capacitor 
Ans: F 
Q: dipole moment 
Ans: Cm 
Q: intensity of the electric field 
Ans: NC-1 or Vm-1 
Q: electric potential energy 
Ans: J 
Q: permitiovity of free space 
Ans: C2N-1m-2 
Q: electric flux 
Ans: Nm2C-1 
Q: permitivity of the medium 
Ans: C2N-1m-2 
Q: torque acting on the dipole 
Ans: Nm 
Q: relative permitivity of the medium 
Ans: no unit 
Q: surface charge density of the conductor 
Ans: Cm-2 
Q: electric potential at a point 
Ans: V 
Q: linear charge density of the conductor 
Ans: Cm-1 

可能是while True:現在我加入了if not atest在循環的結束