+ 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?
1 Antwort
+ 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 :)