简体   繁体   中英

Syntax Error in if statement with operators

I'm very new to python and was wondering what is wrong with this code:

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)

this is the error I get when trying to run this code:

    if operation == +:
                     ^
SyntaxError: invalid syntax

I could not find any problem of this sort on this forum. Please forgive me if this is a dumb question.

Python expects to receive strings as stdin input, so try

if operation == '+':

Also, your num1 , num2 are also going to be strings, so you'll need to call

num1 = int(num1) 

or

num1 = float(num1) 

on them to make them integers or floats (and same with num2 )

Each of your operators must be in quotes as you are recieving a character input. Besides, you must convert your input numbers to int or float

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM