Pull it Together -> unsupported operand type(s) for -: 'str' and 'int'


#1

I get this after [save&submit my code]:
Oops, try again. trip_cost('Charlotte', 0) raised an error: unsupported operand type(s) for -: 'str' and 'int'


I don't know then how can I make 'stri' and 'int' supported operand.
What part am I missing?

   idef hotel_cost(days):
    return 140 * days

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
    else:
        return cost

def trip_cost(days, city):
   print rental_car_cost(days) + plane_ride(city) + hotel_cost(days)

#2

Try it with

def trip_cost(days, city):
   print "{0} {1} {2}".format(rental_car_cost(days),\
       plane_ride_cost(city),hotel_cost(days))
   print """or"""
   print str(rental_car_cost(days))+" "+\
         str(plane_ride_cost(city))+" "+\
         str(hotel_cost(days))

trip_cost(3,"Tampa")

#4

It's not working. Also I didn't learn to use { } in python so far.

After applying your code, it says
"Oops, try again. trip_cost('Pittsburgh', 7) raised an error: unsupported operand type(s) for -: 'str' and 'int'"


#5

I figured it out! At the end, for the def trip_cost(city, days), I had to define the function first, but instead tried to print it without even defining what it should be like. I changed print to return, and it worked. :smiley:

 def hotel_cost(days):
    return 140 * days

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
    else:
        return cost

def trip_cost(city, days):
    **return** hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city)

#6

Please present the ACTUAL code you are using...

My guess

1

the Function trip_cost was defined as having 2 parameter's days and city
and the call of the trip_cost function would be
trip_cost(7,'Pittsburgh')

2

unsupported operand type(s) for -: 'str' and 'int'"
This error occurs if you try to concatenate =int=eger Values using the +-sign
like
print rental_car_cost(days) + plane_ride(city) + hotel_cost(days)
to be able to do this you have to convert-the-integer-to-string using str()
like
print str(rental_car_cost(days)) +" "+ str(plane_ride(city)) +" "+ str(hotel_cost(days))


#8


def hotel_cost(nights):
    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): 
    cost=days*40
    if days>=7:
            cost -=50          
    elif  days >=3:
            cost-=20   

    return cost        
        
    
def trip_cost(city,days): 
    print  str(rental_car_cost(days)) +" "+ str(hotel_cost(days)) +" "+ str(plane_ride_cost(city))

Error appears:

Oops, try again.
trip_cost('Charlotte', 8) returned None instead of the correct value 1573

#9

@arcninja56229,
Please edit your Post by adding 3 backticks ```
in a separate code-line above of your code and behind your code....
Then present the indentation you are using ....!!!

In your trip_cost(city,days) function...
`- you are trying to do an addition on strings...remove the str() method
- they want you to use a return statement.....


#10

Hi, i beg your pardon. I found out that task is not quite clear.
For me works next:

return  rental_car_cost(days) +hotel_cost(days) + plane_ride_cost(city)

I have solved my problem


#11

hi there anyone who is getting an error regarding the trip cost function compare it with the following code -

def hotel_cost(days):
return days*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):
rent = 40*days
if days >= 7:
return rent - 50
elif days >= 3:
return rent - 20
else:
return rent

def trip_cost(city,days):
return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)

This code will give guaranteed answer . give it a try


#12

def hotel_cost(nights):
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):
if days >= 7:
return (40*days) - 50
elif days >= 3:
return (40*days) - 20
else:
return 40*days

def trip_cost (days,city):
return str(hotel_cost(days)) + str(rental_car_cost(days)) + str(plane_ride_cost(city))

Hey buddy, here is my code but there is something wrong, it said " Oops, try again. trip_cost('Pittsburgh', 0) raised an error: unsupported operand type(s) for -: 'str' and 'int' ". Please give me some instruction. Thank your very much.


#13

@systemrockstar11856,
As will want to return the =total-cost= as an =integer=
you might try to use

return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city)

but i think in this case you could try to change your parameter-order
as the code-checker is probably calling the trip_cost() function
like
trip_cost('Pittsburgh', 0)

======================================================

the FUNCTION talk

def myFunc( param1, param2):
    # Begin of =myFunc= FUNCTION-BODY
    # this =myFunc= function- has 2 PARAMETERS param1 and param2
    # param1 and param2 PARAMETERS are used 
    # as -local- VARIABLES throughout the =myFunc= FUNCTION-BODY
    print( param1 + " and " + param2 )
    #End of =myFunc= FUNCTION-BODY

If you want to call/execute the myFunc function
you will have to add a pair of parentheses to myFunc
like
myFunc()
As the myFunc function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren")

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++ function with 1 parameter using return-statement

def myFunction( param1 ):
    # //Begin of =myFunction= FUNCTION-BODY
    # //=myFunction= function has 1 PARAMETER param1
    # //this param1 PARAMETER is used as a -local- VARIABLE
    # //throughout the =myFunction= FUNCTION-BODY
    return param1;
    # //End of FUNCTION-BODY

You have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the =myFunction= FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 )

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the print-method to do a display.

theResult = myFunction( 4 )
print theResult

OR directly

print myFunction( 4 )

#14

From what I've read, the confusion is they don't want you to say hotel_cost(nights) because now nights is equal to days. they now want the trip_cost(city, days) defining function to return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city)

in your code you have:
return str(hotel_cost(days)) + str(rental_car_cost(days)) + str(plane_ride_cost(city))

change to:
return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city)


#15

@datablaster45933,
after reading the function talk
your sentence

From what I've read, the confusion is they don't want you to say hotel_cost(nights) because now nights is equal to days

It might be confusing,
but they are using the days parameter of the trip_cost() function
( a parameter is used as a =local= variable throughout the =function-body= of trip_cost() )
using this =local= variable days
as an argument in the call of your hotel_cost() function
like
hotel_cost(days)


#17

how come when I put
def trip_cost(days, city), it doesnt work.

But it works when I switch the city and days arguments?
def trip_cost(city, days)
shouldnt the order not matter?


#18

but even i have typed the same and i am not getting it:cry:


#19

Thank you so much. I did the same and it works. I agree with you that order should not matter.