2017-01-31 8 views
0

Ich bin nur ein Anfänger zu python.Und ich habe hier nur ein einfaches Programm geschrieben, nur um mich selbst zu bewerten und versuchen, die Fragen in zufälliger Reihenfolge zu beantworten. Aber der Fehler hier ist, dass der Randint Funktion erwirbt manchmal die gleiche Nummer, die bereits erworben wurde, dass ich die gleiche Frage wiederholt bekomme. Ich versuchte mein Bestes, um es zu beheben, aber ich konnte nicht. Hoffe, dass ich etwas Hilfe bekommen here.`import zufälligePython-randint Funktion ohne Wiederholung

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

benutze 'random.shuffle (list)' und du bekommst eine Liste mit zufälliger Reihenfolge - und dann kannst du Elemente mit 'for' loop bekommen. – furas

Antwort

1

vor Ihrer Funktion, fügen Sie eine Reihe von booleans, die die Fragen bereits beantwortet beschreibt:

already_asked = [False] * len(names) 

dann, wo Sie QNO zu einem zuweisen Wert, Zufallszahlen halten, bis Sie einen getroffen haben Sie nicht vorher gefragt haben, und die neu gestellte Frage markieren, wie gefragt:

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

Dies ist die richtige Antwort. Deepak, Sie verbieten die 'randint()' Funktion, Ihnen einen Wert zu geben, den Sie vorher erhalten haben. Um dies zu tun, müssen Sie die zurückgegebenen Werte jedoch im Auge behalten, um sie nicht wieder zurückgeben zu können. – mmenschig

+0

Vielen Dank @ AdamVenis. Obwohl ich etwas Zeit verbringen muss, um die Logik dahinter zu verstehen, das ist, was ich erwartet habe. Du hast meine erste Frage beantwortet ....! <3 <3 –

0

Wenn Sie Randomint (0, 14) verwenden, werden Sie viele Wiederholungen erhalten! Zum Beispiel:

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))) 

Hier ist meine Ausgabe, auf meinem ersten Versuch:

12 
6 
6 
6 
7 
14 
7 
11 
4 
10 

Beachten Sie, wie ich die Nummer 6 dreimal bekam! Offensichtlich werden Wiederholungen weniger ausgeführt, wenn die Entfernung zunimmt, aber Sie werden immer eine Chance haben, Zahlen zu wiederholen, besonders da dies nur eine Pseudozufallszahl ist.

Vielleicht suchen Sie etwas wie Shuffle? Zum Beispiel:

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

vielleicht ein wenig fortgeschritten, seine schöne Möglichkeiten zu haben, die explizite Indexierung nicht

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 

erfordern könnte while True: nun sein, dass ich die if not atest am Ende der Schleife hinzugefügt

Verwandte Themen