7. Taking a Vacation - User Input

#1

I've complete this section, but I was trying to figure out how to get the user to input data and then use that to populate trip_cost.

I used raw_input() to generate the input field, but despite entering the parameters, nothing was sent to the arguments in trip_cost. It caused an error: function trip_cost at 0x7f157e18dcf8

Here is my code:

city = raw_input("Which city will you be traveling to?")
days = raw_input("How many days will you be staying?")
nights = raw_input("How many nights will you be staying?")
spending_money = raw_input("How much extra money will you need?")

def hotel_cost(nights):
return 140 * nights
print hotel_cost

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

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

print trip_cost

Anyone have any ideas? I could see this working as a very basic trip tool, but not if I have to supply the info on the back end.

Thanks!

#2

You can try this

``````import re

input_pattern = re.compile(r'[a-zA-Z]+(\s+\d{1,3}){2}\s+\d{1,6}')

print('Enter the city, number of days, nights and spending money')
print("eg 'Tampa 90 400 50'")
user_input = raw_input() # input() for python3.x users

while(not re.match(input_pattern,user_input)):
print("Usage: string int int int")
print("Enter the city, number of days, nights and spending money")
user_input = raw_input() # input() for python3.x users

user_input = user_input.split()
print ("Hotel cost is \$%s"%(hotel_cost(int(user_input[2]))))
print ("Total trip cost is \$%s"%(trip_cost(user_input[0], int(user_input[1]), int(user_input[3]))))``````

Here is a sample run

``````Enter the city, number of days, nights and spending money
eg 'Tampa 90 400 50'
Tampa 90 400 50
Hotel cost is \$56000
Total trip cost is \$16420``````

Just one little handle you need to add here

``````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
else:
return 0``````

#3

Have you been able to test it successfully?

And I forgot to make one note. i noticed that when you call the method you simply call the method names like `plane_ride_cost`. this is not how to call methods

imagine i have a method

``````def call_on_me(arguments):
#execute some statement
return arguments``````

Assuming the method is supposed to take a number for an argument then I can call the method like this

``call_on_me(9)``

but you probably will not see the result so you have to pass this call as a token(argument) to the print method like this

``print call_on_me(9)``

#4

Hello! Thank you for responding so quickly. I have not had a chance to test your previous code, mainly because I don't understand it. Essentially the question I was asking is why doesn't the `raw_input` data populate the arguments, and is there a way to do so using the code I've already written? Or is this the best solution, and I'd need to scrap what I have?

Thank you for info on calling a method, though. So to make sure I understand you, would I need to include the following code to call `trip_cost`:

``trip_cost(city, days, spending_money)``

where the arguments in parentheses are actual values supplied by the user's input?

PS Is there a formatting guide for this site? It's challenging posting things that are formatted correctly, but your answers are nicely done!