Adding something extra to 7/7


#1

Hello, I wanted to add prompts to this code. They all work apart from the city variable, i get this error when I input ‘Tampa’.

Traceback (most recent call last):
File “python”, line 1, in
File “”, line 1, in
NameError: name ‘Tampa’ is not defined

I feel like i’m missing something simple here can anyone offer some advice. Thank you

city = input("Where would you like to go? ")
nights = input("How many nights would you like to stay? ")
days = input('How many days would you like to rent a car? ')
spending_money = input('And how much spending money will you be taking? ')

def hotel_cost(nights):
return 140 * nights

def plane_ride_cost(city):
if city == str(“Charlotte”):
return 183
elif city == str(“Tampa”):
return 220
elif city == str(“Pittsburgh”):
return 222
elif city == str(“Los Angeles”):
return 475
total_cost = 40
def rental_car_cost(days):

if days >= 7:
discount_cost = total_cost * days - 50
return discount_cost
elif days >= 3:
discount_cost = total_cost * days - 20
return discount_cost
else:
return days * total_cost

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

print trip_cost(city, days, spending_money)


#2

I’ve worked it out sort of

city = input("Where would you like to go? ")
nights = input("How many nights would you like to stay? ")
days = input('How many days would you like to rent a car? ')
spending_money = input('And how much spending money will you be taking? ')

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):
total_cost = 40
if days >= 7:
discount_cost = total_cost * days - 50
return discount_cost
elif days >= 3:
discount_cost = total_cost * days - 20
return discount_cost
else:
return days * total_cost

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

print trip_cost(city, days, spending_money)

I just need to work out how to write the string in the input without having to write it “Tampa”


#3

Hi @pyblaster04012,

If you are using Python 2.x, which is what Codecademy offers in its exercises, use raw_input instead of input. Use input with Python 3.x.

In Python 2.x, the Python interpreter attempts to evaluate your response to the input prompt as Python code. If you enter Tampa, it looks for a variable named Tampa.

With raw_input, your response is considered to be a str. You can convert it to an int or a float when the intent is to use it as a number. You would perform such a conversion in order to assign it to your days or spending_money variable, for example.


#4

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

def cost(spending):
return spending

def trip_cost(city,days,spending_money):
return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)+cost(spending_money)
print(trip_cost(“Los Angeles”,5,600))


#5

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