【问题标题】:How to increment a variable within a python if statement [closed]如何在python if语句中增加变量[关闭]
【发布时间】:2021-09-11 19:03:48
【问题描述】:

我正在尝试根据 if 语句中的条件递增变量。每次我尝试这样做时,我都会在等号上得到一个语法错误,我不知道为什么。我在 Python IDLE 中写这个

You can click on this link to access the image of the code

【问题讨论】:

  • 1) Python 不支持大括号,只需要缩进定义块即可; 2) 发布文字,而不是图片
  • 您是否要创建dict

标签: python macos if-statement syntax


【解决方案1】:

Python 不需要 { 在条件短语的开头和 } 在它的结尾。删除它们并重试:)

【讨论】:

  • 谢谢!这行得通!我把 Python 和 Java 搞混了 :)
【解决方案2】:

你正在用不正确的语法编写 python。

在 python 中,我们使用缩进来表示 for 和 while 循环的开始,而不是括号('{' 和 '}')。

您的代码应如下所示(缩进至关重要):

if var == 9:
    num = num + 1
    print(num)

【讨论】: