New course! Every coder should learn Generative AI!
Try a free lesson0
[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>
8 Answers
+ 4
Your indentation is wrong.
It should look like this:
if...
...
elif...
....
elif
....
+ 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.
+ 2
for example by asking with an if-condition:
if operand not in ('+', '-', '*' [and so on]) :
print('Wrong input!')
+ 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")
+ 1
Try to cast the input to int
and remove the int from print function
Check below
https://code.sololearn.com/c54yj37FYSBQ/#py
+ 1
how do i check if the user has enterend in numbers and/or an acceptable operand
+ 1
Thankyou so much 😄😄
0
So it's the print statement that needs to be indented