[英]Syntax Error in if statement with operators
我对 python 很陌生,想知道这段代码有什么问题:
num1 = input("Please Eneter A Number")
num2 = input("Please Enter Another Number")
operation = input("Please Enter An Operation You Want To Do (example: +, -, *, /): ")
if operation == +:
print(num1 + num2)
if operation == -:
print(num1 - num2)
if operation == /:
print(num1 / num2)
if operation == *:
print(num1 * num2)
这是我在尝试运行此代码时遇到的错误:
if operation == +:
^
SyntaxError: invalid syntax
我在这个论坛上找不到任何此类问题。 如果这是一个愚蠢的问题,请原谅我。
Python 期望接收字符串作为 stdin 输入,所以尝试
if operation == '+':
此外,您的num1
、 num2
也将是字符串,因此您需要调用
num1 = int(num1)
或者
num1 = float(num1)
在它们上使它们成为整数或浮点数(与num2
相同)
当您收到字符输入时,您的每个运算符都必须用引号引起来。 此外,您必须将输入数字转换为 int 或 float
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.