【问题标题】:Why does python say colon is expected but I already have one? [duplicate]为什么python说冒号是预期的,但我已经有了一个? [复制]
【发布时间】:2021-11-26 01:15:59
【问题描述】:

enter image description here

请帮忙...谢谢

【问题讨论】:

  • 请将代码放在问题正文而不是图片上
  • 使用elif作为条件
  • 它不会让我,因为我是一个新用户
  • else 不希望有任何条件,要么像 @HKG 所说的那样使用 elif 或删除它
  • 我试过elif,也没用

标签: python


【解决方案1】:

因为else 没有条件,所以它被它之前的内容所暗示。

a = 4
b = 3
if a < b:
 print("hello")
else:   # <-- no condition
 print("bye")

check this out 获取教程,您还可以在其中找到有关 elif 的信息

【讨论】:

    【解决方案2】:

    您甚至不必在“其他”部分提供条件, 这就像,
    例如

    if ( a > 100):
        #[Apply changes or operations under a condition if a is greater than 100.]
    
    else:
        #[Apply changes or operations under a condition if a is less than or equal to 100.].
    

    如果想在此处添加条件,则只需使用 'elif' 而不是 'else'。

    【讨论】:

      【解决方案3】:

      问题实际上不在于缺少冒号。它认为缺少冒号,因为应该在“else”之后立即有一个冒号。相反,您有“totalRevenue...”。

      请记住,else 不应该有条件 - else 应该表示所有其他条件中未涵盖的剩余部分。

      如果你想有一个条件,你应该使用elif而不是else

      【讨论】:

        猜你喜欢
        • 2020-11-15
        • 2023-03-27
        • 2021-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-25
        • 1970-01-01
        相关资源
        最近更新 更多