[python][help] illegal target for varible annotation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[python][help] illegal target for varible annotation

not sure what im doing wrong here? I'm trying to reference variable "operand" to get a math function to perform the sum. only just started learning python variables havent learn if statements yet <code> num1 = input("enter the first number") operand = input(" what operation would you like to use") num2 = input("please enter your second number") if operand == "+": print(int(num1)+int(num2)) elif operand == "-": print(int(num1)-int(num2)) elif operand == "*": print(int(num1)*int(num2)) elif operand == "x": print(int(num1)*int(num2)) elif operand == "X": print(int(num1)*int(num2)) elif operand == "/": print(int(num1)/int(num2)) elif operand == "%": print(int(num1)%int(num2)) </code>

14th Mar 2019, 12:45 PM
MaxBanter
MaxBanter - avatar
8 Answers
+ 4
Your indentation is wrong. It should look like this: if... ... elif... .... elif ....
14th Mar 2019, 1:07 PM
HonFu
HonFu - avatar
+ 3
Yes, but all the other elifs need to be aligned, because they stand in relation to each other. Whenever there's a colon, what follows needs to be indented.
14th Mar 2019, 1:10 PM
HonFu
HonFu - avatar
+ 2
for example by asking with an if-condition: if operand not in ('+', '-', '*' [and so on]) : print('Wrong input!')
14th Mar 2019, 2:01 PM
HonFu
HonFu - avatar
+ 2
For numbers you can check it creating a finction as below : def inputNumber(message): while True: try: userInput = int(input(message)) except ValueError: print("Not an integer! Try again.") continue else: return userInput break So your code will be modified as num1 = inputNumber("enter the first number\n") operand = input("what operation would you like to use\n") num2 = inputNumber("please enter your second number\n")
14th Mar 2019, 2:07 PM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 1
Try to cast the input to int and remove the int from print function Check below https://code.sololearn.com/c54yj37FYSBQ/#py
14th Mar 2019, 1:14 PM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 1
how do i check if the user has enterend in numbers and/or an acceptable operand
14th Mar 2019, 1:38 PM
MaxBanter
MaxBanter - avatar
+ 1
Thankyou so much 😄😄
14th Mar 2019, 2:22 PM
MaxBanter
MaxBanter - avatar
0
So it's the print statement that needs to be indented
14th Mar 2019, 1:09 PM
MaxBanter
MaxBanter - avatar