如何从列表中随机选择一个变量,然后将其重新分配给另一个变量?

2024-09-30 06:32:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个智囊团游戏,我相信你们都知道它是什么,某种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")


Tags: ofto游戏numberinputifeasy数字
2条回答

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

正如blorgon的答案所提到的,变量必须在左边,输入必须在右边。在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")

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

相关问题 更多 >

    热门问题