While starting to learn Python, I found a problem when trying to program a calculation using split. If I were to have whitespaces in my string
15 - 3
then the program runs fine.
If I didn't have whitespaces in my string
15-3
then I get an error message because split expected 3 values (the two values and the operator). I tried to compensate for it by using this:
num1, operator, num2 = input("Enter calculation: ").split()
if not num1.isdigit() or not num2.isdigit():
print("Please use whitespaces in calculation")
else:
num1 = int(num1)
num2 = int(num2)
if operator == "+":
print("{} + {} = {}".format(num1, num2, num1+num2))
elif...
However, this still doesn't catch the error if I don't use whitespaces. What am I doing wrong?