Seperating int & str then addition? (Phyton) | Sololearn: Learn to code for FREE!
+ 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?

4th Jul 2020, 12:01 PM
YAKUP KARAKAŇě
YAKUP KARAKAŇě - avatar
1 Resposta
+ 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 :)
4th Jul 2020, 2:46 PM
YAKUP KARAKAŇě
YAKUP KARAKAŇě - avatar