4/7 Taking a vacation... Odd errors but says I did it right


#1

http://pastebin.com/49UP7ztD

city = raw_input("Type the city you're visiting." )
    nights = raw_input("How many nights will you be staying for?")
    def hotel_cost(nights):
        if nights:
            print
            return 140 * nights
    def plane_ride_cost(city):
        if city == "Charlotte":
            print
            return 183
        elif city == "Tampa":
            print
            return 220
        elif city == "Pittsburgh":
            return 222
            print
        elif city == "Los Angeles":
            return 475
            print
        else:
            return "Please enter a valid location/amount of nights"
     
    print plane_ride_cost
    print hotel_cost
     
    days == raw_input("Enter number")
    def rental_car_cost(days):
        rent = 40 * days
        if days >= 7:
            return rent - 50
        elif days >= 3:
            return rent - 20
        else:
            "please enter a number"
        return
    print rental_car_cost
Messages shown in console:
Type the city you're visiting. Tampaa
How many nights will you be staying for? 3
<function plane_ride_cost at 0x1857f50>
<function hotel_cost at 0x1857c08>
Enter number 3
<function rental_car_cost at 0x184f050>
None

#2

Haha, you're discovering memory pointers early by accident :wink:

What you're seeing there "<function xyz at pointer>" means that you printed out the function definition (memory location) itself rather than executing it.

Try calling the function(s), instead.

E.g. print plane_ride_cost(some_args, here) and so on.

Does this make sense?


#3

Yeh that makes sense, so would you use that function to locate specific pieces of code or if the code fails you can force it to print the location of failed code?


#4

Well, no. You'd use memory pointers in more advanced code where optimization is key. It's speedier to refer to memory than to refer to a variable and resolve it.

You shouldn't worry about it for now :smile:


#5

Ah alright, reckon you could over look my code? Now that I sorted out the correct 'print'. the raw_input is not working correctly and doing some bizzar stuff.

New Console errors...

Type the city you're visiting. Tampa
How many nights will you be staying for? 0
Please enter a valid location/amount of nights
TampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampaTampa
Enter number 7
Traceback (most recent call last):
File "python", line 35, in
File "python", line 27, in rental_car_cost
TypeError: unsupported operand type(s) for -: 'unicode' and 'int'


#6

When you assign the variable days you're using the == comparison operator versus the = assignment operator. Might be the issue in itself.

I see it's repeating Tampa a lot. Did you change the code at all? Looks like a loop.


#7

I may have done actually... I just copied this code over. The issue turns out to be that when I multiply the raw_input values... It's literally just copying it instead of multiplying the number values.
Would it work if I added a function to ensure only numerical values are placed? So it would read it as a number instead of like a letter?

Console code-: After Print fix...

Type the city you're visiting. Tampa
How many nights will you be staying for? 3

220

33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
Enter number 3
Traceback (most recent call last):
File "python", line 37, in
File "python", line 31, in rental_car_cost
TypeError: unsupported operand type(s) for -: 'unicode' and 'int'


#8

Ah, ok. After looking it up on the Python documentation, raw_input() returns a string, not an integer therefore the repeating string makes sense. It's not actually using the input as a number, but as text.

To fix that, either use input() to have Python 2 convert it to an integer for you or embrace the future and use the int() function to convert it to an integer.

Like so:

something = raw_input("Enter a number!")
something = int(something); //reassign as int rather than as string

That will work. (Can also one-line it something = int(raw_input("Enter a number!")))

Further, looks like at the end it's still the same problem with plane_ride_cost().

How's that work for you?


#9

Finally did it, I tried to us Int although it didn't work for me (might have used it wrong) but float worked just as well as int.

Thank you so much for the help and if you would like for your own benefit, i'll upload a updated model of the code to help show the changes I made and why.

    city = raw_input("Type the city you're visiting."
    )
    nights = float(raw_input("How many nights will you be staying for?"))
    def hotel_cost(nights):
        if nights:
            return 140 * nights
    def plane_ride_cost(city):
        if city == "Charlotte":
            print
            return 183
        elif city == "Tampa":
            print
            return 220
        elif city == "Pittsburgh":
            return 222
            print
        elif city == "Los Angeles":
            return 475
            print
        else:
            return "Please enter a valid location/amount of nights"
     
    print "Flight and hotel cost: "
    print plane_ride_cost(city) + hotel_cost(nights)
     
    days = float(raw_input("Amount of days you're renting vehicle?"))
    def rental_car_cost(days):
        rent = 40 * days
        if days >= 7:
            return rent - 50
        elif days >= 3:
            return rent - 20
        else:
            return rent
    print"Rental car costs: "
    print rental_car_cost(days)
    print "Overall cost: "
    print rental_car_cost(days) + plane_ride_cost(city) + hotel_cost(nights)

#10

Great job! Looks great.


#11

This is the easiest one i have and it works.

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