Python bug

RichM499

Daemon Poster
Messages
783
Location
Spaceballs Mega-Maid
sg2u.png


Code:
def hotel_cost(nights):
    c = nights * 140
    return c
def plane_ride_cost(f):
    if f == "Charlotte":
        return 183
    elif f == "Tampa":
        return 220
    elif f == "Pittsburgh":
        return 222
    else:
        return 475
def rental_car_cost(days):
    cost=days*40
    if days == 0:
        days = 40
    elif days >= 7:
        return cost - 50
    elif days >= 3:
        return cost - 20
    else:
        return cost
def trip_cost(city,days, spending_money):
    return trip_cost
    total = rentalcarcost(days)+planeridecost(city)+hotelcost(days)
    return total + spending_money
    
print trip_cost("Los Angeles", 5,000)

This is the code. I'm having trouble figuring out why it's not working. it gives me
"<function trip_cost at 0x7f4b440ae578>
None " as output and allows me to pass this 'section' of the website I'm using to try and learn it but I can't figure out what is wrong with my code. Could anyone here help please?
 

Attachments

  • sg2u.png
    sg2u.png
    34.9 KB · Views: 1
Last edited by a moderator:
Well, we know which function it's in.
Code:
def trip_cost(city,days, spending_money):
    return trip_cost
    total = rentalcarcost(days)+planeridecost(city)+hotelcost(days)
    return total + spending_money

I've never done Python before, but it looks like "return trip_cost" might be the culprit to me.

the at 0x7f4b440ae578 refers to a memory address. If ran through a debugger it would probably tell you which line is generating this

EDIT: I was probably wrong. Doing a crash course in python right now, brb :tongue:

EDIT 2!: Okay, a few things I've noticed is inconsistent function naming. rental_car_cost and rentalcarcost on lines 13 and 24 as well as the other ones on line 24.

and print is (I believe) called like this:
print(trip_cost("Los Angeles", 5, 1000))
 
Last edited:
I tried your suggestions and got the same error. Nice catch on the naming consistency I didn't notice that. I think my print line should have worked as well but I tried yours too


Code:
def hotel_cost(nights):
    c = nights * 140
    return c
def plane_ride_cost(f):
    if f == "Charlotte":
        return 183
    elif f == "Tampa":
        return 220
    elif f == "Pittsburgh":
        return 222
    else:
        return 475
def rental_car_cost(days):
    cost=days*40
    if days == 0:
        days = 40
    elif days >= 7:
        return cost - 50
    elif days >= 3:
        return cost - 20
    else:
        return cost
def trip_cost(city,days, spending_money):
    return trip_cost
    total = rental_car_cost(days)+plane_ride_cost(city)+hotel_cost(days)
    return total + spending_money
print(trip_cost("Los Angeles", 5, 1000))
 

Attachments

  • yShLfLg.png
    yShLfLg.png
    47.8 KB · Views: 1
Last edited:
Remove "return trip_cost" and it should work.

that line exits the trip_cost function before any values are computed, and therefore render the two lines after it useless :tongue:

Also, just an FYI, this site uses BBCode markup instead of HTML for forum posts. so [ and ] work in place of < and > for most things.

EDIT: Also, I just noticed on lines 15 and 16.. Should 16 be "cost = 40"?
 
Last edited:
still failed. I removed the return
Code:
def hotel_cost(nights):
    c = nights * 140
    return c
def plane_ride_cost(f):
    if f == "Charlotte":
        return 183
    elif f == "Tampa":
        return 220
    elif f == "Pittsburgh":
        return 222
    else:
        return 475
def rental_car_cost(days):
    cost=days*40
    if days == 0:
        days = 40
    elif days >= 7:
        return cost - 50
    elif days >= 3:
        return cost - 20
    else:
        return cost
def trip_cost(city,days,spending_money):
    total = rental_car_cost(days)+plane_ride_cost(city)+hotel_cost(days)
    return total + spending_money
    
    

print trip_cost("Los Angeles", 5, 1000)





""""Oops, try again! trip_cost should take exactly three parameters: city, days, and spending_money (in that order). """
 

Attachments

  • nKi9mQx.png
    nKi9mQx.png
    30.3 KB · Views: 1
Last edited:
Looks like it's outputting fine to me. What's wrong with it?
That's the same output I get
Code:
Loading source files >>> done
Checking for libraries... done
Building... done
Storing binaries <<< done
Listing build files... done
Checking for application data... done
Running:
------------------------------
 
2355
 
------------------------------
Syncing application data <<< done
Application Exited

However, I get a compilation error if my print doesn't have ()'s around it. I see yours doesn't, though :tongue:
 
I bet this website is just brken that I'm trying to learn on! LOl. Do you know any resources that I can test code and work in an environment that will help correct mistakes???? I'd really like to begin making a video game in python but I am new to it. I'll definitely remember your syntax for later in case it's needed for the newer version
 
Well, I've been doing this on this site:
https://compilr.com/
One of my favorites, especially because it doesn't require any downloads or installations.

The only thing that will help you correct mistakes is a good IDE with syntax and function detection (ie, it'll underline if a function hasn't been called, if it's mispelled, etc). I don't know of any for Python, just google around until you find one you like.

For the stuff you're doing, though, I'd say a site like Compilr would work perfect. It spits out errors and tells you which line they're on and the type of error (ie, syntax error in the case of my print with / without ()'s)
 
Back
Top Bottom