Seperating int & str then addition? (Phyton) | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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