Invalid literal for int() with base 10: ''


#1



https://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096#


Oops, try again. An error was raised for rental_car_cost(1): invalid literal for int() with base 10: ''


When I run my code it works fine, as in all the numbers expected show and all calculations are done as expected. however I am still getting this error (10 was the amount of days i hired a car)

I'm stuck. can anyone shed some light on what the problem is?


def hotel_cost(nights):
    nights = int(raw_input('How many nights are you staying?:'))
    return nights * 140
    
def plane_ride_cost(city):
    city = raw_input('Where are you flying to?:')
    if city == 'Charlotte':
        return 183
    elif city == 'Tampa':
        return 220
    elif city == 'Pittsburgh':
        return 222
    elif city == 'Los Angeles':
        return 475

def rental_car_cost(days):
    days = int(raw_input('How many days would you like to rent a car for?:'))
    cost = days * 40
    if days >= 7:
        return cost - 50
    elif days >= 3:
        return cost - 20


hbill = 'Hotel Bill = ' + str(hotel_cost(nights))
fbill = 'Air Fare = ' + str(plane_ride_cost(city))
cbill = 'Car Hire = ' + str(rental_car_cost(days))


print hbill
print fbill
print cbill
print 'Total = ' + str(total)


#2

'' is an empty string:

>>> int('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''

Your functions are doing things that are very far away from what they are meant to be doing, involving int


#3

Hi,

thanks for the reply. sorry i don't understand what I should do as the output I get looks good but don't underststand where the error is.

here is the output

How many nights are you staying?: 6
Where are you flying to?: Tampa
How many days would you like to rent a car for?: 4
Hotel Bill = 840
Air Fare = 220
Car Hire = 140
Total = 1850
None


#4

Where could your functions possibly be getting empty strings from and call int on them? There's only one situation where you use int, and it's not to do something that the instructions describe, it drastically changes their behaviour.


#5

nights = int(raw_input('How many nights are you staying?:'))
city = raw_input('Where are you flying to?:')
days = int(raw_input('How many days would you like to rent a car for?:'))

def hotel_cost(nights):
    return nights * 140

def plane_ride_cost(city):
    if city == 'Charlotte':
        return 183
    elif city == 'Tampa':
        return 220
    elif city == 'Pittsburgh':
        return 222
    elif city == 'Los Angeles':
        return 475

def rental_car_cost(days):
    cost = days * 40
    if days >= 7:
        return cost - 50
    elif days >= 3:
        return cost - 20


hbill = 'Hotel Bill = ' + str(hotel_cost(nights))
fbill = 'Air Fare = ' + str(plane_ride_cost(city))
cbill = 'Car Hire = ' + str(rental_car_cost(days))
total = hotel_cost(nights) + plane_ride_cost(city) + rental_car_cost(days)

print hbill
print fbill
print cbill
print 'Total = ' + str(total)

any user input is a string and needs to be converted to an int for it to do math calcs? or at least thats what I thought.
I've tweaked the code to above and tried it in idle and it works fine but on here i am now getting the following error:

Oops, try again. It looks like rental_car_cost returns None instead of the correct amount (40) for 1 days.

I take it there isn't an error in the code but its looking for it to be done a different way?


#6

When you read text then it'll be a string, yes. And text is what comes from stdin, at least that's how things commonly work (nothing truly stopping the computer from allowing users to enter cat pictures)

You might have seen functions written like this in school:

f(x) = x + 1

Now imagine if that suddenly took "user input" from some other source than its argument.

That's not how a function should behave.

Python doesn't enforce purity of functions, but the more pure the better, pure functions are much easier to reason about as they don't have side-effects.

Your new error isn't a crash, no. It just behaves incorrectly. Which is what the previous thing was as well, but the incorrect behaviour led to a crash.

So you'd start by reproducing what it describes, call that function with the argument it mentions. Decide whether you agree that it's misbehaving and then investigate where it is straying from the actions that it should be doing in order to arrive at the desired result for that input


#7

def rental_car_cost(days):
    cost = days * 40
    if days >= 7:
        return cost - 50
    elif days >= 3:
        return cost - 20
    else:
        return cost

so it turns out all that was stopping it from passing was that I had missed out the else: return cost.


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.