Develop a Python function which either returns the float square of its parameter x if the parameter is a number, or prints the string "Sorry Dave, I'm afraid I can't do that" if the parameter is a string, and then returns 0.0.
What am I doing wrong? I'm a first year CS student and I have no previous programming background.
I created a function that takes user input, evaluates what type of input it is and print different out puts for number and strings.
For that I used eval(var)
func. I also the type(var) == type
to verify the type and a if-else
loop.
def findt():
userin = input("Input: ") # Takes user input
inpeval = eval(userin) # Evaluates input type
if type(inpeval) == int: # If input is an int
userfloat = float(inpeval) # Modifies into a float
print(userfloat ** 2) # Prints the square of the value
elif type(inpeval) == float: # If input is a float
print(inpreval ** 2) # Prints the square of the value
elif type(userin) == str: # If input is a string
print("Sorry Dave, I'm afraid I can't do that") # Print a string
return 0.0 # Return 0.0
else:
print("Invalid Input")
findt()
When I run my code it works well when input is an int, a float or a char. But if I write more than one char it returns me an error:
NameError: name 'whateverinput' is not defined.
You're trying to eval
input before you know it's needed. Get rid of it entirely:
def findt():
userin = input("Input: ") # Takes user input
if type(userin) == int: # If input is an int
userfloat = float(userin) # Modifies into a float
...
The root problem is that you can't evaluate an undefined name. If your input is a string that is not the name of an object in your program, it will fail. eval
requires everything you feed it to be defined.
Here is a better way to achieve your goal by using the string method isnumeric()
to test if the input is numeric or not.
def findt():
userin = input("Input: ")
if userin.isnumeric():
# userin is numeric
result = float(userin) ** 2
print(result)
else:
try:
# userin is a float
result = float(userin) ** 2
print(result)
except ValueError:
# userin is a string
print("Sorry Dave, I'm afraid I can't do that")
return 0.0
findt()
Update: a concise version:
def findt():
userin = input("Input: ")
try:
# userin is an int or float
result = float(userin) ** 2
print(result)
except ValueError:
# userin is a string
print("Sorry Dave, I'm afraid I can't do that")
return 0.0
findt()
I found the solution for my problem. The way I did it I take the input from the user and i try
to convert it to a float. If it is a number it will convert and print a float that is the square of the input. If the input is a string it cannot be converted to a float and will give me an error so I use an except ValueError:
to print the string I want and return 0.0.
def whattype():
user_input = input(">>> ")
try:
user_input = float(user_input)
print(user_input ** 2)
except ValueError:
print("Sorry Dave, I'm afraid I can't do that")
return 0.0
whattype()
Thank you all for the suggestions and help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.