Python Write a program which gives the option between doing 3 things: 1) Add two
ID: 3851459 • Letter: P
Question
Python
Write a program which gives the option between doing 3 things:
1) Add two numbers
2) Multiply three numbers
3) Multiply a number and a string
Ask the user to choose an option. Output “Invalid option” if the user enters something apart from 1, 2 or 3. Otherwise depending on what the user enters ask the user again for inputs to perform the required choice and output the answer.
Sample program output 1: You can choose between: 1) Adding two numbers 2) Multiplying three numbers 3) Multiplying a number and a string Please choose an option: 2 Please enter number 1: 2 Please enter number 2: 3 Please enter number 3: 5 The answer is 30
Explanation / Answer
def Add():
print("Please enter number 1:")
num1=int(input())
print("Please enter number 2:")
num2=int(input())
print("Addition of numbers is:"+(str(num1+num2)))
def MultiplyThreeNum():
print("Please enter number 1:")
num1=int(input())
print("Please enter number 2:")
num2=int(input())
print("Please enter number 3:")
num3=int(input())
print("Addition of numbers is:"+(str(num1*num2*num3)))
def MultiplyNumAndString():
print("Please enter number 1:")
num1=int(input())
print("Please enter string:")
num2=input()
print("Addition of numbers is:"+(str(num1*num2)))
if __name__ == "__main__":
# options = {1:Add,2:MultiplyThreeNum,3:MultiplyNumAndString}
while True:
print(" Menu:")
print("1) Add two numbers")
print("2) Multiply three numbers")
print("3)Multiply a number and a String")
print("4)Exit")
print("Select an option:")
try:
c=int(input())
if c == 1:
Add()
elif c == 2:
MultiplyThreeNum()
elif c == 3:
MultiplyNumAndString()
elif c == 4:
break
except Exception as e:
print(e)
# options[c]()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.