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?
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 :)