【问题标题】:How do I randomly select a variable from a list, and then re assign it to another variable?如何从列表中随机选择一个变量,然后将其重新分配给另一个变量?
【发布时间】:2021-05-06 20:26:44
【问题描述】:

我想做一个策划游戏,我相信你们都知道它是什么,某种 GCSE 风格的 Python 猜数字游戏。它应该要求您输入一个数字,选择一个数字,然后告诉您是否猜对了,但我可能无法将随机选择的数字分配给“x”。

如您所见,我对 python 还是很陌生。

numberseasy = ['1244', '1354', '2355', '2366', '2609', '0010', '1234', '8873', '7775', '2512', '0293', '9463', '9901',
               '6272', '0629']
numbershard = ['25356', '86025', '67390', '96873', '01255', '77654', '96756', '88742', '09564', '12345', '19455',
               '35656', '20967', '32570']

print("welcome to mastermind!")
gamemode = input("please select gamemode: easy, hard")
if gamemode == "easy":
    (random.choice(numberseasy)) = x
    print("easy was selected")
    print("im thinking of a number, try to guess a one digit integer each time to work out the number im thinking of i")
    print("will tell you if you have one correct")
    first = input("please enter a number")

【问题讨论】:

    标签: python list random assign


    【解决方案1】:

    您应该对文本的输入字符串使用.lower()。这将允许以任何大写字母输入文本。

    正如博尔根的回答所提到的,您必须在左侧有变量,在右侧有输入。在python中,它是从右到左的,例如cake = Truefood = "cake"

    numberseasy = ['1244', '1354', '2355', '2366', '2609', '0010', '1234', '8873', '7775', '2512', '0293', '9463', '9901',
                   '6272', '0629']
    numbershard = ['25356', '86025', '67390', '96873', '01255', '77654', '96756', '88742', '09564', '12345', '19455',
                   '35656', '20967', '32570']
    
    print("welcome to mastermind!")
    gamemode = input("please select gamemode: easy, hard")
    if gamemode.lower() == "easy":
        x = random.choice(numberseasy)
        print("easy was selected")
        print("im thinking of a number, try to guess a one digit integer each time to work out the number im thinking of i")
        print("will tell you if you have one correct")
        first = input("please enter a number")
    

    【讨论】:

      【解决方案2】:

      变量分配从左到右。你只需要x = random.choice(numberseasy)

      【讨论】:

      • @StealthReb 不客气。如果您对任何给出的答案感到满意,您应该将其中一个标记为正确!
      猜你喜欢
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多