I want to eliminate " " from user input


Taking a Vacation: 3. Getting there

For this portion of code, I want the user to input the city, and then have the correct cost returned. The problem is that currently, the code will only run if the user inputs the city with quotation marks (ex "Tampa" works, Tampa does not). If the city name is not input with " ", the result is an error that says '(city name) not defined'

Ideally, the user could just input the name and the code would work

This is the error message:
Traceback (most recent call last):
File "python", line 11, in
File "", line 1, in
NameError: name 'Tampa' is not defined

Replace this line with your code. 

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
print plane_ride_cost(city)


codecademy runs python2, use raw_input() in python2. using input() in python2 is tricky, and unnecessary (difficult)

codecademy stores raw_input as unicode, a simply string casting should solve this problem:

city=str(raw_input("Which city would you like to go? "))


Worked like a charm! Thank you!


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