【问题标题】:how to add integer input in a set object如何在集合对象中添加整数输入
【发布时间】:2019-08-18 16:06:13
【问题描述】:

无法将项目添加到将其作为用户输入的集合中

input>>j=set()
input>>j.add(int(input()))

4

TypeError: 描述符“add”需要一个“set”对象但收到一个“int”

【问题讨论】:

标签: python-3.7


【解决方案1】:

两种方法:

  1. j = set()
    j.add(a) # a 可以是任何东西。如果你想要整数,你可以用 int() 进行类型转换

  2. j = {''} # 以这种方式初始化集合需要一些默认参数,否则 python 会将其设为字典
    j.add(1)
    j.add(a) # 其中 a 是整数
    j.remove('') #删除我们添加的初始字符串

【讨论】:

    【解决方案2】:

    因为你必须调用它才能得到一个集合

    j = set()
    j.add(int(input()))
    

    【讨论】:

    • 我称它为 ...j=set()
    • 我提供的代码可以正常工作。如果您有错误消息,则根本原因在其他地方。你能提供更多你拥有的代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2016-05-31
    • 2018-11-19
    • 2019-01-18
    相关资源
    最近更新 更多