I am trying to write a very simple code which I will feed it two numbers (with the input function) and it'll give me a result based on these numbers. When the numbers are just numbers (e.g., 12 or 15) everything works fine, but when instead of a number is, for example, an addition (i.e., 6 + 6) then I get an error saying "could not convert string to float: '6 + 6'.
Please see below a reproducible example:
def calc_sal(number1, number2):
return number1 * 4.5 + number2
number1 = float(input("Give number1 "))
number2 = float(input("Give number2 "))
print(calc_sal(number1, number2))
and this is the error:
Give number1 6 + 6
Traceback (most recent call last):
File "SalaryCalc.py", line 4, in <module>
number1 = float(input("Give number1 "))
ValueError: could not convert string to float: '6 + 6'
It appears to me that I cannot perform an addition through the input function, instead it thinks that it's a string "6 + 6". However, when I am running the same code line by line in an interactive session, everything works just fine (i.e., when the input is 6 + 6, the output is 12, rather than "6 + 6"). So do you why is this happening only when I am running the programme through the terminal and not interactively? And how to resolve this?
Thank you.