【问题标题】:I don't see where i am supposed to at the ':'我看不到我应该在':'
【发布时间】:2022-01-14 17:33:17
【问题描述】:
a = raw_input("What do you want to add to the list?"
              " If you would like to remove something from the list type 'remove' : ")
if str(a) not == "remove".lower():
    print('"'), str(a), ('"' " has been added to list")
    list1.append(a)
    print(list1)

当我运行它时(有更多代码,但这是相关位)它在“if str(a) not == "remove".lower(): “ 线。请记住,这是在 python 2.7 上

【问题讨论】:

  • 语法错误。使用!= 而不是not ==。这在基本教程中都有介绍。

标签: python-2.7


【解决方案1】:

在 Python 中测试不等式是有效的:

A != B

所以是这样的:

not (A == B)

但这不是:

A not == B    # syntax error

(如果您一直在使用 in 运算符,您可能会认为它是这样工作的,因为 not (A in B)A not in B 等效的。)

一旦你解决了这个问题,就知道了:

print('"'), str(a), ('"' " has been added to list")

是被诅咒的,只是在 Python 2 中意外地做了你想做的事情(它不会做你显然认为它在任何当前支持的 Python 版本中所做的事情)。当您想print() 某事时,要打印的内容的规范以第一个) 结束。以下适用于 Python 2 和 Python 3:

print( '"{}" has been added to list'.format(a) )

然后事实是,您将.lower() 应用于比较的错误一侧(对于已经总是小写的东西)。

【讨论】:

  • 我假设我会让它 print(('"'), str(a), ('"' " has been added to list")) (我以为我以前有过,但是哦,好吧。也谢谢!
  • @N018B 我说得太早了。你写的 will 实际上可以在 Python 2.x 中工作,但它的语法很容易引起误解。答案已更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多