Why won't it recognize my input?


#1



Taking a Vacation - Getting There

Hi I've been playing around with the taking a vacation module and I can't seem to get my code to work...? I don't understand why when I type in one of the cities defined it says it is not defined???
it's probably a very simple answer...
thank you in advance for your response(s)!


Traceback (most recent call last):
File "python", line 19, in
File "", line 1, in
NameError: name 'Charlotte' is not defined


def hotel_cost(nights): #returns the cost of the hotel
    return 140 * nights

nights = input("enter number of nights:")

print "total hotel cost:", hotel_cost(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"
    else:
        return ("I don't know the cost for that city sorry")
city = input("enter destination city please:")
print plane_ride_cost(city)


#2

in python2, input() will evaluate the input as code


#3

do I have to put raw input??


#4

given what you want to achieve, yes


#5

thank you i am trying it out!
on to the next problem :wink:


#6

I'm trying this now :
trying to make it so that it will recognize the city even if you put it in capitals or lowercase etc. it doesn't return an error message but it just returns the else statement for every input
ie. "enter number of nights: 4
total hotel cost: 560
enter destination city please: charlotte
I don't know the cost for that city sorry
None "

any idea what im doing wrong?
thanks

def hotel_cost(nights): #returns the cost of the hotel
    return 140 * nights

nights = input("enter number of nights:")

print "total hotel cost:", hotel_cost(nights)

def plane_ride_cost(city):
    if city.upper == "CHARLOTTE":
        return 183
    elif city.upper == "TAMPA":
        return 220
    elif city.upper == "PITTSBURGH":
        return 222
    elif city.upper == "LOS ANGELES":
        return 475
    else:
        return ("I don't know the cost for that city sorry")
city = raw_input("enter destination city please:")
print plane_ride_cost(city)

#7

.upper() is a method, it requires parentheses. personally, i would just convert the user input once to uppercase, much more efficient:

city = raw_input("enter destination city please:").upper()

#8

perfect thank you very much!


#9

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