python - Python 中 else 语句的语法
问题描述
我对 python 很陌生,我的 else 语句不断出现语法错误。
我将评论语法所在的else 。
print("""
(A)ddition
(S)subtraction
(D)ivision
(M)multiplication
""")
operation = input("select an operation from above (initials) = ")
if(operation == "A","S","D","M"):
#this is where i am getting syntax
else:
print("select valid operation.")
number1 = int(input("first number = "))
number2 = int(input("second number = "))
if(operation == "A","M","D","S"):
if operation == "A":
print("this is the result = ", number1+number2)
elif operation == "S":
print("this is the final result", number1 - number2)
elif operation == "M":
print("this is the final result", number1 * number2)
elif operation == "D":
print("this is the final result", number1/number2, ".And this is the remainder = ",number1&number2)
解决方案
您不必缩进 else 语句。
if:
else:
推荐阅读
- wpf - 选择单选按钮组项目时,WPF 菜单不会关闭
- flutter - ListView 中的颤动边框更改移动下面的项目
- python - jupyter notebook 的当前工作目录设置为 vscode 中的临时文件夹
- selenium - 如何配置无头 Chromedriver 以模拟悬停设备?
- java - 在春季数据绑定期间,是否有另一种方法可以在列表中实例化抽象类的具体实例?
- javascript - Bootstrap 和 Popper 的 WordPress JS 加载问题
- javascript - 比较两个对象,如果对象键值对的一部分存在则返回 true
- geojson - 将 GeoJSON 转换为 DbGeometry
- ios - UIViewPropertyAnimator 在调用 finishAnimation(at:) 后在 dealloc 上崩溃
- blazor-server-side - 如何在连接到 Internet 的本地计算机的 Blazor 服务器中测试发送电子邮件