+ 1
Seperating int & str then addition? (Phyton)
For example: we had the input â12+12â from user. How to separate this and code for math addition?
4 Answers
+ 1
if I code something like this:
command = input(âenter a number (number - operator - number): â)
parseCommand = command.split()
if len(parseCommand) != 3:
print(âinvalid entryâ)
exit(0)
try:
number1 = int(parseCommand[0])
operator = parseCommand[1]
number1 = int(parseCommand[2])
except ValueError:
print(âyou need to enter a numeric valueâ)
exit(0)
if operator == â+â:
print(command, â=â, number1 + number2)
elif operator == â-â:
print(command, â=â, number1 - number2)
elif operator == â*â:
print(command, â=â, number1 * number2)
elif operator == â/â:
print(command, â=â, number1 / number2)
# this code splits with spaces (12 + 12) and calculates the values entered.
# but how can I integrate the code you gave me here if there is no spaces between the characters that user entered.
#thank you so much. also a while circle would be more appreciated :)