Python Forum
Error in Python script
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.
Reply


Messages In This Thread
Error in Python script - by garvind25 - May-23-2020, 07:40 PM
RE: Error in Python script - by buran - May-23-2020, 07:53 PM
RE: Error in Python script - by garvind25 - May-23-2020, 09:31 PM
RE: Error in Python script - by GOTO10 - May-23-2020, 10:56 PM
RE: Error in Python script - by garvind25 - May-24-2020, 05:15 PM
RE: Error in Python script - by pyzyx3qwerty - May-24-2020, 05:34 PM
RE: Error in Python script - by buran - May-24-2020, 05:47 PM
RE: Error in Python script - by GOTO10 - May-24-2020, 09:04 PM
RE: Error in Python script - by garvind25 - May-25-2020, 06:02 PM
RE: Error in Python script - by buran - May-25-2020, 06:34 PM
RE: Error in Python script - by GOTO10 - May-25-2020, 08:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Re-write BASH script to Python script popi75 5 2,445 Apr-30-2021, 03:52 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020