Есть ли способы оптимизировать это?Python

Программы на Python
Anonymous
Есть ли способы оптимизировать это?

Сообщение Anonymous »

Asks the user what operation would they like to use.
operation = input('What operation would you like?\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n')
Addition
if operation == '1': print("You chose addition!\n") first_number = input("What's the first number?\n") second_number = input("What's the second number\n") first_number = int(first_number) second_number = int(second_number) total = (first_number + second_number) print(total)
Subtraction
elif operation == '2': print("You chose Subtraction!\n") first_number = input("What's the first number?\n") second_number = input("What's the second number?\n") first_number = int(first_number) second_number = int(second_number) total = (first_number - second_number) print(total)
Multiplication
elif operation == '3': print("You chose Multiplication!\n") first_number = input("What's the first number?\n") second_number = input("What's the second number?\n") first_number = int(first_number) second_number = int(second_number) total = (first_number * second_number) print(total)
Division
elif operation == '4': print("You chose Division!\n") first_number = input("What's the first number?\n") second_number = input("What's the second number?\n") first_number = int(first_number) second_number = int(second_number) total = (first_number / second_number) print(total)
Tells the user that the operation they called is not available
else: print("Operation not available.")

I'm looking for a shorter code, especially for the variables first_number and second_number. Is there any way to make them an integer from the start without type casting it?


Источник: https://stackoverflow.com/questions/780 ... imize-this

Вернуться в «Python»