Python 错误:TypeError:Random.choice() 需要 2 个位置参数,但给出了 9 个

问题描述 投票:0回答:1
import random
names = random.choice("James", "Max", "Steven", "Sarah", "Luther", "Ben", "David", "Michelle")
print(names)

这是我输入的内容,上面写着 Python 错误:TypeError:Random.choice() 需要 2 个位置参数,但给出了 9 个。

我尝试将“选择”更改为“randstr”或“randint”,但仍然不起作用。但我希望它能从“名称”中生成一个随机名称。

python decoding
1个回答
0
投票

将所有名字放入一个列表中。将列表作为唯一参数传递给 random.choice()

import random

names = ["James", "Max", "Steven", "Sarah", "Luther", "Ben", "David", "Michelle"]
name = random.choice(names)
print(name)
© www.soinside.com 2019 - 2024. All rights reserved.