首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError: randint()采用3个位置参数,但给出了4个

TypeError: randint()采用3个位置参数,但给出了4个
EN

Stack Overflow用户
提问于 2017-10-18 10:49:09
回答 3查看 14.2K关注 0票数 0

我正在为一个学校项目写一个石头,纸和剪刀机器人。我总是在标题(TypeError: randint() takes 3 positional arguments but 4 were given)中出现错误,我不知道为什么。我的密码在下面。

代码语言:javascript
运行
复制
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%。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-21 04:46:43

random.randint只使用两个参数,一个开始和一个结束。Python提到的第三个参数是self,它是自动完成的。

要在1和3之间选择一个数字,只需执行random.randint(1,3)

顺便说一句,这些if语句没有任何意义;它们应该类似于:

代码语言:javascript
运行
复制
if userInput == "paper":
    # send discord messages
票数 3
EN

Stack Overflow用户

发布于 2021-03-31 07:34:16

如果需要多个随机数,也可以使用numpy.random.randint(start,stop-1,number of randoms)

票数 2
EN

Stack Overflow用户

发布于 2022-10-12 16:56:18

我也有同样的问题,我正在构建一个猜号脚本,因为我提供了1到10之间的所有数字,我认为你应该把它格式化为random.randit(1,3),而不是在中间列出变量。如果您使用(1,3),它也说明了这两个数字之间的变量。

-edit我没有看到有人已经回答了这个问题,如果你还有问题我很乐意帮忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46808438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档