Question
Conversion and exception handling python3 Create a function that asks the user to enter a number and returns the number. function name: get_num parameters: prompt
Conversion and exception handling python3
Create a function that asks the user to enter a number and returns the number.
function name: get_num parameters: prompt (string) returns: int or float operation: + Use the "prompt" parameter as the parameter to an input() call in order to ask the user for a number. + Append a ": " to the prompt (i.e., assume the user didn't put a colon in the prompt). + Convert the number to a int using the int() function and return this number from the function. BUT. - If the user enters something that cannot be converted using the int() function, try again to convert it to a float() and return this number from the function. - If THAT fails, print "invalid input" and keep asking until a valid number is entered. - Note that a while True loop and exception handling is a really simple way of doing this. * expected output:
>>> get_num("Enter a number") Enter a num: 6 # RETURNS 6
>>> get_num("Enter your age") Enter your age: 55 # RETURNS 55
>>> get_num("What is your weight") What is your weight: NA invalid input What is your weight: five pounds invalid input What is your weight: 200 # RETURNS 200
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started