Вам не нужны ни функции, ни переменные sum
или multiply
, просто поместите операцию в str.format()
, и вы пропустили последнюю позицию.
# def addition(num1,num2):
# return num1+num2
# def multiplication(num1,num2):
# return num1*num2
print("1.addition")
print("2.multiplication")
choice = int(input("Enter Choice 1/2"))
num1 = float(input("Enter First Number:"))
num2 = float(input("Enter Second Number:"))
# sum = float(num1)+float(num2)
# multiply = float(num1)*float(num2)
if choice == 1:
print("additon of {0} and {1} is {2}".format(num1,num2, num1 + num2))
elif choice == 2:
print("additon of {0} and {1} is {2}".format(num1, num2, num1 * num2))
И знайте, чтоВы можете использовать fstrings (> = Python 3.6):
if choice == 1:
print(f"additon of {num1} and {num2} is {num1 + num2}")
elif choice == 2:
print(f"additon of {num1} and {num2} is {num1 * num2}")
Форматирование в старом стиле:
if choice == 1:
print("additon of %s and %s is %s" % (num1,num2, num1 + num2))
elif choice == 2:
print("additon of %s and %s is %s" % (num1, num2, num1 * num2))
Или объединение строк:
if choice == 1:
print("additon of " + num1 + " and " + num2 + " is " + (num1 + num2))
elif choice == 2:
print("additon of " + num1 + " and " + num2 + " is " + (num1 * num2))
Каждый человек делаетэто другой способ, и иногда полезно знать их все.