In: Computer Science
Write for Python IDLE 3.8.5
Write functions:
i) One that prompts a user for 2 numbers.
ii) Adds the two numbers if they are even
iii) Multiplies the two numbers if they are odd
iv) Displays the appropriate output to the user
You are writing 4 functions and calling them to test functionality.
# Prompt on command to enter input
# returns both input
def promptTwoNumbers():
x = int(input("Enter 1st Number : "))
y = int(input("Enter 2nd Number : "))
return [x,y]
# returns sum of two integers
def addTwoNumbers(x, y):
return x+y
# returns multiplication of two integers
def multTwoNumbers(x, y):
return x*y
# displays the result
def displayOutput(res):
print(res)
# gets both inputs
print("Enter two Odd or two Even Numbers ")
x, y = promptTwoNumbers()
# checks whether both even or both odd
if x%2 ==0 and y%2 ==0:
ans = addTwoNumbers(x, y)
res = "Sum of " + str(x) + " and " + str(y) + " = " + str(ans)
displayOutput(res)
elif x%2 == 1 and y%2 == 1:
ans = multTwoNumbers(x, y)
res = "Multiplication of " + str(x) + " and " + str(y) + " = "+str(ans)
displayOutput(res)
else:
print("Please Enter Valid Input")
Output #1:
Output #2: