使用后如何丢弃随机数?
how to discard a random number after it has been used?
我正在编写一个程序来测试我的化学知识。
但我希望随机数出现一次,就不会出现第二次。
比如数字1出现时(氢),就不会出现两次。
怎么做?
---我使用的编程语言是python---
我的英文不好,所以有语法错误,请见谅
感谢您的帮助
from random import randint
computer = randint(1,20)
loop = True
if computer == 1:
computer = 'Hidro'
if computer == 2:
computer = 'Heli'
if computer == 3:
computer = 'Liti'
if computer == 4:
computer = 'Beri'
if computer == 5:
computer = 'Bo'
if computer == 6:
computer = 'Cacbon'
if computer == 7:
computer = 'Nitơ'
if computer == 8:
computer = 'Oxi'
if computer == 9:
computer = 'Flo'
if computer == 10:
computer = 'Neon'
if computer == 11:
computer = 'Natri'
if computer == 12:
computer = 'Magie'
if computer == 13:
computer = 'Nhôm'
if computer == 14:
computer = 'Silic'
if computer == 15:
computer = 'Photpho'
if computer == 16:
computer = 'Lưu Huỳnh'
if computer == 17:
computer = 'Clo'
if computer == 18:
computer = 'Agon'
if computer == 19:
computer = 'Kali'
if computer == 20:
computer = 'Canxi'
while loop:
print('Nguyên tố:' + computer)
choose = input('Nguyên Tử Khối:' )
if computer == 'Hidro':
if choose == '1':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Heli':
if choose == '4':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Liti':
if choose == '7':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Beri':
if choose == '9':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Bo':
if choose == '11':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Cacbon':
if choose == '12':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Nitơ':
if choose == '14':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Oxi':
if choose == '16':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Flo':
if choose == '19':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Neon':
if choose == '20':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Natri':
if choose == '23':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Magie':
if choose == '24':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Nhôm':
if choose == '27':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Silic':
if choose == '28':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Photpho':
if choose == '31':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Lưu Huỳnh':
if choose == '32':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Clo':
if choose == '35,5':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Agon':
if choose == '39,9':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Kali':
if choose == '39':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Canxi':
if choose == '40':
print('Đúng!')
else:
print('Học lại đi mày')
if choose == 'End':
exit()
要随机化问题出现的顺序,您可以生成一个随机列表,其中包含与您的问题编号相对应的范围内的唯一整数。
首先,创建一个空列表。创建一个循环,只要列表长度小于您的问题总数,就会生成一个随机数。在每次迭代中,检查随机数是否在列表中。如果没有将其附加到列表中。然后,使用这些值来随机化您的问题编号:
randomOrder = [];
while len(randomOrder) < 20:
randomNum = randint(1, 20)
if randomNum not in randomOrder:
randomOrder.append(randomNum)
然后您可以使用 dict
或 list
来存储问题并将它们映射到随机问题顺序。
最简单的方法是:
- 创建词典列表;
- 从列表中随机选择一个元素;
- 删除元素;
- 循环重复该过程,直到列表为空(使用 len() 函数确定列表的长度)。
这是一个包含三个元素的最小示例:
import random
elements = [
{"Hidro": 1},
{"Heli": 2},
{"Liti": 3}
]
while len(elements) > 0:
selection = random.choice(elements)
print(elements)
print(selection)
elements.remove(selection)
输出(由于随机性,您的可能会有所不同):
[{'Hidro': 1}, {'Heli': 2}, {'Liti': 3}]
{'Liti': 3}
[{'Hidro': 1}, {'Heli': 2}]
{'Heli': 2}
[{'Hidro': 1}]
{'Hidro': 1}
您可以使用 索引符号 进入列表,您可以使用 关键字 从字典中 select 元素。例如,elements[0]
将是 {'Hidro': 1}
(因为 {'Hidro': 1}
在 elements
的索引 0 处),而 elements[0]["Hidro"]
将是 1(因为 1 是键,"Hidro"
)。有了这个,您可以消除代码中的大部分冗余。
正如评论中指出的,您需要熟悉列表和词典。
我正在编写一个程序来测试我的化学知识。 但我希望随机数出现一次,就不会出现第二次。 比如数字1出现时(氢),就不会出现两次。 怎么做? ---我使用的编程语言是python--- 我的英文不好,所以有语法错误,请见谅 感谢您的帮助
from random import randint
computer = randint(1,20)
loop = True
if computer == 1:
computer = 'Hidro'
if computer == 2:
computer = 'Heli'
if computer == 3:
computer = 'Liti'
if computer == 4:
computer = 'Beri'
if computer == 5:
computer = 'Bo'
if computer == 6:
computer = 'Cacbon'
if computer == 7:
computer = 'Nitơ'
if computer == 8:
computer = 'Oxi'
if computer == 9:
computer = 'Flo'
if computer == 10:
computer = 'Neon'
if computer == 11:
computer = 'Natri'
if computer == 12:
computer = 'Magie'
if computer == 13:
computer = 'Nhôm'
if computer == 14:
computer = 'Silic'
if computer == 15:
computer = 'Photpho'
if computer == 16:
computer = 'Lưu Huỳnh'
if computer == 17:
computer = 'Clo'
if computer == 18:
computer = 'Agon'
if computer == 19:
computer = 'Kali'
if computer == 20:
computer = 'Canxi'
while loop:
print('Nguyên tố:' + computer)
choose = input('Nguyên Tử Khối:' )
if computer == 'Hidro':
if choose == '1':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Heli':
if choose == '4':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Liti':
if choose == '7':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Beri':
if choose == '9':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Bo':
if choose == '11':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Cacbon':
if choose == '12':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Nitơ':
if choose == '14':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Oxi':
if choose == '16':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Flo':
if choose == '19':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Neon':
if choose == '20':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Natri':
if choose == '23':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Magie':
if choose == '24':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Nhôm':
if choose == '27':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Silic':
if choose == '28':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Photpho':
if choose == '31':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Lưu Huỳnh':
if choose == '32':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Clo':
if choose == '35,5':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Agon':
if choose == '39,9':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Kali':
if choose == '39':
print('Đúng!')
else:
print('Học lại đi mày')
if computer == 'Canxi':
if choose == '40':
print('Đúng!')
else:
print('Học lại đi mày')
if choose == 'End':
exit()
要随机化问题出现的顺序,您可以生成一个随机列表,其中包含与您的问题编号相对应的范围内的唯一整数。
首先,创建一个空列表。创建一个循环,只要列表长度小于您的问题总数,就会生成一个随机数。在每次迭代中,检查随机数是否在列表中。如果没有将其附加到列表中。然后,使用这些值来随机化您的问题编号:
randomOrder = [];
while len(randomOrder) < 20:
randomNum = randint(1, 20)
if randomNum not in randomOrder:
randomOrder.append(randomNum)
然后您可以使用 dict
或 list
来存储问题并将它们映射到随机问题顺序。
最简单的方法是:
- 创建词典列表;
- 从列表中随机选择一个元素;
- 删除元素;
- 循环重复该过程,直到列表为空(使用 len() 函数确定列表的长度)。
这是一个包含三个元素的最小示例:
import random
elements = [
{"Hidro": 1},
{"Heli": 2},
{"Liti": 3}
]
while len(elements) > 0:
selection = random.choice(elements)
print(elements)
print(selection)
elements.remove(selection)
输出(由于随机性,您的可能会有所不同):
[{'Hidro': 1}, {'Heli': 2}, {'Liti': 3}]
{'Liti': 3}
[{'Hidro': 1}, {'Heli': 2}]
{'Heli': 2}
[{'Hidro': 1}]
{'Hidro': 1}
您可以使用 索引符号 进入列表,您可以使用 关键字 从字典中 select 元素。例如,elements[0]
将是 {'Hidro': 1}
(因为 {'Hidro': 1}
在 elements
的索引 0 处),而 elements[0]["Hidro"]
将是 1(因为 1 是键,"Hidro"
)。有了这个,您可以消除代码中的大部分冗余。
正如评论中指出的,您需要熟悉列表和词典。