TypeError:randint()需要3个位置参数,但有4个被给出
问题描述:
我正在为学校项目编写一个岩石,纸和剪刀机器人。我不断收到标题中的错误(TypeError: randint() takes 3 positional arguments but 4 were given
),我不知道为什么。我的代码如下。TypeError:randint()需要3个位置参数,但有4个被给出
if userInput : 'rock'
choice = random.randint(1,2,3)
if choice == 1:
await client.send_message(message.channel, embed=RockEmbed)
elif choice == 2:
await client.send_message(message.channel, embed=PaperEmbed)
elif choice == 3:
await client.send_message(message.channel, embed=ScissorsEmbed)
if userInput : 'scissors'
choice2 = random.randint(1,2,3)
if choice2 == 1:
await client.send_message(message.channel, embed=RockEmbed)
elif choice2 == 2:
await client.send_message(message.channel, embed=PaperEmbed)
elif choice2 == 3:
await client.send_message(message.channel, embed=ScissorsEmbed)
if userInput : 'paper'
choice3 = random.randint(1,2,3)
if choice3 == 1:
await client.send_message(message.channel, embed=RockEmbed)
elif choice3 == 2:
await client.send_message(message.channel, embed=PaperEmbed)
elif choice3 == 3:
await client.send_message(message.channel, embed=ScissorsEmbed)
我说random.randint(1,2,3)
,这显然是3个参数,而不是4.我,很肯定我的语法是正确的,但不是100%。
答
random.randint
只需要两个参数,一个开始和一个结束。 Python提到的第三个参数是self
,这是自动完成的。
要选择1到3之间的数字,请执行random.randint(1,3)
。
那些if
声明没有任何意义,顺便说一下;他们应该看起来像这样:
if userInput == "paper":
# send discord messages
请注意'random.randint()'是一个方法,它有一个'self'参数作为参数之一。如果您传入3个参数,则总共有4个参数。阅读[函数文档](https://docs.python.org/3/library/random.html#random.randint)以查看它接受的参数。 –
质量过滤器是有原因的。你的解决方法不被赞赏。你不需要发布太多的代码;只是'random.randint(1,2,3)'已经显示错误。 –
我链接的文档非常清楚该函数的功能。你不能传入3个参数。使用'random.randint(1,3)'或使用'random.choice([1,2,3])'。 –