【问题标题】:When I add a number "a" into a set, it return with an error [closed]当我将数字“a”添加到集合中时,它会返回错误 [关闭]
【发布时间】:2022-01-22 11:55:02
【问题描述】:

我正在编写一个 python 代码,我可以将许多数字添加到一个集合中,然后打印它。我不知道为什么它会返回错误。这是我的代码:

ask = int(input('How many numbers you want to add in a set?'))
sett = set()
while ask:
    a = int(input('Enter the number:')
    sett.add(a)
    ask = ask-1
    if ask == 0:
        print(sett)
        break

但是它返回了这个错误:
‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎   文件“Untitled5.py”,第 5 行
sett.add(a)
^
SyntaxError: 无效语法

所以任何人都可以找到错误的原因吗?这个问题可能很容易回答,但请回答。

【问题讨论】:

  • 上一行缺少右括号。投票结束是一个琐碎的错字。
  • 你忘了关闭括号:int(input('Enter the number:'))

标签: python


【解决方案1】:

你忘了关闭括号:

ask = int(input('How many numbers you want to add in a set?'))
sett = set()
while ask:
    a = int(input('Enter the number:'))
    sett.add(a)
    ask = ask-1
    if ask == 0:
        print(sett)
        break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 2016-10-16
    • 2021-12-03
    相关资源
    最近更新 更多