我只是python的初学者。我在这里写了一个简单的程序,只是为了自我评估自己,并尝试以随机顺序回答问题。但这里的错误是randint函数有时会获得相同的数字我已经获得了相同的问题。我尽力解决它,但我不能。希望我能在这里得到一些帮助。随机进口
function destroyer(arr, param2, param3) {
// Remove all the values
console.log("---");
console.log("arr: " + arr);
var args = Array.from(arr);
console.log(args);
var in_i = arr[0];
return in_i.filter(function (x) {
if (args.indexOf(x) !== -1) {
return true;
} else {
return false;
}
});
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
答案 0 :(得分:1)
在你的函数之前,添加一组布尔值来描述哪些问题已被回答:
already_asked = [False] * len(names)
然后,在你将qno分配给一个值的地方,继续生成随机数,直到你找到之前没有问过的那个,然后按照要求标记新问题:
qno = random.randint(0, len(names))
while already_asked[qno]:
qno = random.randint(0, len(names))
already_asked[qno] = True
答案 1 :(得分:1)
random.shuffle功能完全符合您的要求。
答案 2 :(得分: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]
答案 3 :(得分: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
现在我在{+ 1}}添加了while True:
在循环结束,可能是if not atest