【问题标题】:how to discard a random number after it has been used?随机数使用后如何丢弃?
【发布时间】:2021-10-30 13:21:49
【问题描述】:

我正在编写一个程序来测试我的化学知识。 但我希望一旦出现随机数,它就不会出现第二次。 例如,当数字 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()

【问题讨论】:

  • 请注意,您应该真正了解dicts,以避免所有这些重复代码。
  • 是的,它总是重复相同的代码
  • 1.列出您要从中挑选的元素。 2. 打乱你的元素列表。 3. 按顺序从随机列表中挑选元素。

标签: python random


【解决方案1】:

最简单的方法是:

  1. 创建字典列表;
  2. 从列表中选择一个随机元素;
  3. 移除元素;
  4. 循环重复该过程,直到列表为空(使用 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}

您可以使用索引符号进入列表,并且可以使用关键字从字典中选择元素。例如,elements[0] 将是 {'Hidro': 1}(因为 {'Hidro': 1} 位于 elements 的索引 0 处),而 elements[0]["Hidro"] 将是 1(因为 1 是键的值 "Hidro")。这样,您可以消除代码中的大部分冗余。

正如评论中指出的,您需要熟悉列表和字典。

【讨论】:

    【解决方案2】:

    要随机化问题出现的顺序,您可以在与您的问题编号相对应的范围内生成唯一整数的随机列表。

    首先,创建一个空列表。只要列表长度小于您的问题总数,就创建一个生成随机数的循环。在每次迭代中,检查随机数是否在列表中。如果没有将其附加到列表中。然后,使用这些值随机化您的问题编号:

    randomOrder = [];
    while len(randomOrder) < 20:
       randomNum = randint(1, 20)
       if randomNum not in randomOrder:
          randomOrder.append(randomNum)
    

    然后您可以使用dictlist 来存储问题并将它们映射到随机问题顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 2021-03-07
      • 2018-06-07
      • 1970-01-01
      相关资源
      最近更新 更多