Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Error in Python script
#1
Hi,

I was trying to write a simple python code with if statement. The code is as below. I was getting the error: “ValueError: could not convert string to float: 'n' ”. What could be the reason? I am pointing towards the error line as indicated by the test bench.

temp = input("Enter your dog's age")
temp = float(temp)  # Error line as indicated by a pre-provided test bench at a online learning site
if(temp > 0.0):
    if(temp <= 1.0):
        dog_age = 15
    elif(temp <= 2): 
        dog_age = temp * 12
    elif(temp <= 3):
        dog_age = temp * 9.3
    elif(temp <= 4):
        dog_age = temp * 8
    elif(temp <= 5):
        dog_age = temp * 7.2
    elif (temp > 5.00):
        dog_age = (5 * 7.2) + ((temp - 5) * 7)
    
dog_age = round(dog_age, 2)
print("The given dog age {} is {} in human years." .format (temp, dog_age))

Error:
ValueError: could not convert string to float: 'n'
Any advice will be deeply appreciated.

Thanks and Regards,
Arvind Gupta.
Quote
#2
obviously one of the test cases is to pass string n.
You need to handle input that is not convertible to float. Your assignment should have instructions for that
Quote
#3
Thanks for the reply. Ok, I tried by modifying the code in following way.

temp = input("Enter your dog's age")

if(type(temp) != float or type(temp) != int):
    print("Your input is incorrect")
    dog_age = 0
    print(type(temp)) # <----here the value of temp is str, even if the input given is 1/1.0
elif(temp >= 0):
      temp = float(temp)
      if(temp <= 1.0):  
        dog_age = 15
        dog_age = round(dog_age, 2)
        print("The given dog age {} is {} in human years." .format (temp, dog_age))
      elif(temp <= 2):
        dog_age = temp * 12
        dog_age = round(dog_age, 2)
        print("The given dog age {} is {} in human years." .format (temp, dog_age))
      elif(temp <= 3):
        dog_age = temp * 9.3
        dog_age = round(dog_age, 2)
        print("The given dog age {} is {} in human years." .format (temp, dog_age))
      elif(temp <= 4):
        dog_age = temp * 8
        dog_age = round(dog_age, 2)
        print("The given dog age {} is {} in human years." .format (temp, dog_age))
      elif(temp <= 5):
        dog_age = temp * 7.2
        dog_age = round(dog_age, 2)
        print("The given dog age {} is {} in human years." .format (temp, dog_age))
      elif(temp > 5.00):
        dog_age = (5 * 7.2) + ((temp - 5) * 7)
        dog_age = round(dog_age, 2)
        print("The given dog age {} is {} in human years." .format (temp, dog_age))

What ever input I am giving, the output is always zero. When i am giving input as 1/1.0 it is taking it as string (I checked it by outputting the value of temp in the beginning). In other words, the first elif loop is never getting executed!

Pls. advice.

Thanks and Regards,
Arvind Gupta.
Quote
#4
The result of input() is always a string, so there is nothing a user can enter that will satisfy your first if statement. You need to convert the input to a float or int as needed. If you are expected to account for invalid input, there are ways of testing whether the user's input can be converted successfully (is not letters instead of numbers, for example), and/or ways of handling any exception that occurs when you try to convert it.
Quote
#5
(May-23-2020, 10:56 PM)GOTO10 Wrote: If you are expected to account for invalid input, there are ways of testing whether the user's input can be converted successfully (is not letters instead of numbers, for example), and/or ways of handling any exception that occurs when you try to convert it.

Thanks for your reply. Yes, I am expected to account for invalid inputs. How to do so pls.?

Regards,
Arvind Gupta
Quote
#6
On line 1
temp = float(input("Enter your dog's age"))
Remove the
temp = float(temp)
from line 8
However, i don't know if the lines 3,4,5,6 are required, as python itself will print the error
Error:
could not convert string to float: 'hello'
However, if it is necessary in your hw to do as you are expected, i'm clueless :(
pyzyx3qwerty
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Quote
#7
@pyzyx3qwerty, it's great you try to help, but if you don't know how to help, at least don't do harm...
If they don't convert the str returned from input(), how they will make the comparison later on in the code.
They simply need to handle the incorrect input. Note, I guess their instructions are pretty clear what they need to do (i.e. what test cases need to be handled), but they don't show the full text of the assignment.
ndc85430 likes this post
Quote
#8
(May-24-2020, 05:15 PM)garvind25 Wrote: Thanks for your reply. Yes, I am expected to account for invalid inputs. How to do so pls.?

Regards,
Arvind Gupta


If your instructor is expecting you to account for invalid inputs, I presume you have been taught a method for doing so. I can't do your homework for you, but here is a generic approach using try/except:
my_string = input() # remember, any value retrieved from input() is a string by default

try: # use try when you will attempt to do something that may result in an error

    my_string_float = float(my_string)

except ValueError: # this line determines what happens if an error of type 'ValueError' occurs

    print('Could not convert my_string to float.')
You will probably want to put the input() and try/except all into a loop, so that you can get new input from the user if the error occurs. If this doesn't give you enough information to complete your assignment, please post the full details of the assignment so that we can help you further.
Quote
#9
Your suggestion did not pass the test bench. Here is the assignment:

"""
“How Old is Your Dog in Human Years?” Calculator: Write a program to calculate a dog’s age in human years.

Ask the user for an age in dog years and calculate that age in human years. Allow for float values but check
the user’s input. You can use the following rules to approximately convert a medium-sized dog’s age to human
years:

• For the first year, one dog year is equal to 15 human years
• For the first 2 years, each dog year is equal to 12 human years
• For the first 3 years, each dog year is equal to 9.3 human years
• For the first 4 years, each dog year is equal to 8 human years
• For the first 5 years, each dog year is equal to 7.2 human years
• After that, each dog year is equal to 7 human years.

Print the result in the following format, substituting for <dog_age> and <human_age>: "The given dog age
<dog_age> is <human_age> in human years."
Round the result to 2 decimal places. Note: If there is a 0 in the hundredths place, you can drop it,
e.g. 24.00 can be displayed as 24.0.

"""
Quote
#10
your assignment is unclear - it does not specify what to do in case of invalid input - e.g. keep asking, or print specific output, etc..
i.e. you can just keep asking the user until valid input, but I guess the prompt will cause the test to fail
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)