Write a method Inverse that asks the user for a number and displays the inverse of that number. For example, the inverse of 2 is 1/2 or 0.5 and the inverse of 3 is 1/3 or 0.333. If the program...

1 answer below »

Write a method Inverse that asks the user for a number and displays the inverse of that number.


For example, the inverse of 2 is 1/2 or 0.5 and the inverse of 3 is 1/3 or 0.333.


If the program encounters an error because the user put something other than numbers, it raises an exception and prints “Please enter a number”.


If it encounters an error because they put zero for the denominator, it raises another exception and prints “Can’t divide by zero.”


Sample output:
>>> Inverse()
Enter a number: 2
Its inverse is 0.5


Enter a number: Five
Please enter a number


Enter a number: 0
Can't divide by zero

Answered Same DayOct 12, 2021

Answer To: Write a method Inverse that asks the user for a number and displays the inverse of that number. For...

Neha answered on Oct 12 2021
125 Votes
def Inverse():
while True:
try:
number = input("Please enter an integer:
")
inverseValue = 1/int(number)
print("Its inverse is: ",inverseValue)
break
except ValueError:
print("No valid integer! Please try again...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here