Pull it together: unexpected EOF while parsing


#1




I'm receiving the following error message:

Oops, try again. hotel_cost(1) raised an error: unexpected EOF while parsing (, line 0)


I'm not really sure what did I do wrong, since the error seems to be on line 0 (?????).
Please help!


def hotel_cost(nights):
    nights = input("How many nights will you be staying? ")
    return 140*nights

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):
    rent = 40 * days
    if days >= 7:
        rent -= 50
    elif days >=3:
        rent -= 20
    return rent
    
def trip_cost(city, days):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city)


#2

Taking input from standard input isn't part of what that function should do

Functions generally only have a single purpose, and if they aren't, they should probably be split up.

Additionally, you'll have an easier time hard-coding input than messing with input/output. You can for example create a list of test-data to be executed by a loop each time you hit run. That does of course make for a useless program, that's a question of what the purpose is.


#3

Hey, @ionatan, thank for taking the time to answer me!

I erased the input part the function and it worked correctly. Funnily enough, I had been using the hotel_cost function that way on the previous exercises and wasn't getting that error message.
Anyway, thanks again for the help!


#4

If you have a make-me-a-sammich function and it fires a nuke, you'll go, no. that's not what this function should do.

It's probably pretty well defined by the instructions how hotel_cost should behave, anything other than that is pretty much unacceptable.

There's nothing to split up here, I meant big ugly functions that do ten different things. There is something to remove though, because it shouldn't be happening at all

The error message is coming from your function trying to get input from standard input, you were also trying to execute the input which is.. even worse (use raw_input, not input. input() is equivalent to eval(raw_input()) and is very rarely something you want to do, at least not if you're well informed about what you're doing and what options you have


#5

Actually, you caused the submission test to crash, which is why it "passed" you.

The test code for this exercise is better behaved (written by me as so happens)


#6

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