python - 如何将多行代码用于 else 语句?

标签 python python-2.7

我正在尝试编写一个简单的数字加法器作为更复杂程序的一部分。我的代码是 if-else 语句的一部分。

else:num1=input("Enter the first number")
    num2=input("Enter the second number")
    print num1 + num2

但是,这会产生错误...

    num2=input("Enter the second number")
   ^
IndentationError: unexpected indent

那么如何创建多行 else 语句呢?

最佳答案

else: 之后换行并缩进所有代码,如下所示:

else:
    num1 = input("Enter the first number")
    num2 = input("Enter the second number")
    print num1 + num2

关于python - 如何将多行代码用于 else 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25213876/

相关文章:

java - 如何在 Java 程序中使用 Python 类?

python - 在 Tkinter 标签文本的末尾显示三个点

python - "math.floor(x)"和 "int(x)"是否在 Python 中对正实数产生不同的结果?

Python 多行正则表达式分隔符

python - 连接神经网络中 split 的密集层 - Keras

python - 比较两个 CSV 文件之间的值并写入第三个 CSV 文件

python - 使用 Python 获取 Windows 8.1 进程列表

python - 随机播放程序(列表索引超出范围错误)

python - 在主分支 merge 时更改 Python 包版本

python - 查找集合列表的组合