1. Here i used dictionary mapping to achieve switch statement.
2. Used format method of the string.
3. Print multiple string in single print method by """.
4. Used while loop to iterate over the user input.
def sum():
try:
number1=float(input('Please enter first Number'))
number2=float(input('Please enter second Number'))
print('Sum is:',number1+number2)
except Exception as e:
print('Invalid value is passed to sum.')
def subtraction():
try:
number1=float(input('Please enter first Number'))
number2=float(input('Please enter second Number'))
subtractionValue=number1-number2
print('Subtraction is:{subtractionValue}'.format(subtractionValue=subtractionValue))
except Exception as e:
print('Invalid value is passed to sum.')
def multiplication():
try:
number1=float(input('Please enter first Number'))
number2=float(input('Please enter second Number'))
print('Multiplication is:{0}'.format(number1*number2))
except Exception as e:
print('Invalid value is passed to sum.')
options={
1:sum,
2:subtraction,
3:multiplication
}
def displayWhatUserCanInput():
print("""
Please enter number
1. For Sum
2. For Subtraction
3. For Multiplication
""")
try:
i=int(input("Enter the option 1,2 or 3"))
func=options.get(i,'Invalid value')
func()
except Exception as e:
print('Invalid entry is passed.')
def whileloop():
displayWhatUserCanInput()
done=False
while done==False:
userinput=input('Enter 5 to exit and any other number to continue.')
if int(userinput)==5:
print('Bye!')
done=True
else:
displayWhatUserCanInput()
done=False
whileloop()
No comments:
Post a Comment