The Bordless Tourist UnicodeEncode Error

Hi everyone, I’m working on this project: https://www.codecademy.com/paths/computer-science/tracks/cspath-cumulative-tourism/modules/cspath-boredless-tourist/projects/the-boredless-tourist

After copying and pasting the attractions from Step 35 and adding print(attractions), it looks like anything with “São Paulo, Brazil” causes an error: UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\xe3’ in position 299: ordinal not in range(128)

Here’s my code, help understanding what I’ve done wrong would be greatly appreciated:

destinations = ["Paris, France", "Shanghai, China", "Los Angeles, USA", "São Paulo, Brazil", "Cairo, Egypt"]
test_traveler = ["Erin Wilkes", "Shanghai, China", ["historical site", "art"]]

def get_destination_index(destination):
  destination_index = destinations.index(destination)
  return destination_index

def get_traveler_location(traveler):
  traveler_destination = traveler[1]
  traveler_destination_index = get_destination_index(traveler_destination)
  return traveler_destination_index

test_destination_index = get_traveler_location(test_traveler)

attractions = [[] for destination in destinations]

def add_attraction(destination, attraction):
  destination_index = get_destination_index(destination)
  try:
    destination_index = get_destination_index(destination)
  except ValueError:
    return
  attractions_for_destination = attractions[destination_index]
  attractions_for_destination.append(attraction)
  return

add_attraction("Los Angeles, USA", ["Venice Beach", ["beach"]])
add_attraction("Paris, France", ["the Louvre", ["art", "museum"]])
add_attraction("Paris, France", ["Arc de Triomphe", ["historical site", "monument"]])
add_attraction("Shanghai, China", ["Yu Garden", ["garden", "historcical site"]])
add_attraction("Shanghai, China", ["Yuz Museum", ["art", "museum"]])
add_attraction("Shanghai, China", ["Oriental Pearl Tower", ["skyscraper", "viewing deck"]])
add_attraction("Los Angeles, USA", ["LACMA", ["art", "museum"]])
add_attraction("São Paulo, Brazil", ["São Paulo Zoo", ["zoo"]])
add_attraction("São Paulo, Brazil", ["Pátio do Colégio", ["historical site"]])
add_attraction("Cairo, Egypt", ["Pyramids of Giza", ["monument", "historical site"]])
add_attraction("Cairo, Egypt", ["Egyptian Museum", ["museum"]])
print(attractions)

Hello, @board5387631890. Welcome to the forum. The ‘special’ characters in your strings are the cause of the error. You can google solutions to be able to use them in your code, or the simple fix for now is to replace them with plain, old, ordinary ‘a’ and ‘e’.

2 Likes

Thanks! I thought perhaps I did something wrong while doing this project.

I’ve replaced the special characters with normal text.

2 Likes

You’re welcome! Hopefully it’s working now. Just FYI for future posts, when you include actual code in your post, be sure to use the </> button to retain the formatting, indentation, etc. See the following:

THANK YOU @patrickd314 for this!!

Code that is posted without the indentations preserved, is not Python. Indentations are not optional in Python, but are rather an essential part of the language , lacking which your code will not run at all.

Unindented code is difficult to read, requires guesswork concerning your intent, and cannot be copied and pasted for testing, both because it is unindented and because it often contains characters such as left-and right double-quotes that are unreadable to the Python interpreter.

The Codecademy forum provides a quick and easy way to post properly-formatted code. All you need to do is look for the </> icon in the menu bar which appears at the top of the text box while you type.

capture

Press Enter to go to a blank line, click that icon, and you will see this:

capture_r

Just copy and paste your code directly from the editor into that highlighted portion, and all of the nicely-readable Python formatting will be preserved! Moreover, it will be in a format that can be directly copied and pasted into a Python IDE for testing.

The same icon can be used for highlighting small code snippets in-line for clarity.

1 Like

Python needs to be told by the environment that it’s OK to use unicode, or it won’t, and then what you have there can’t be written.

$ export LANG=en_US.UTF-8
3 Likes

Thank you,

This character encoding issue was really throwing me off. None of the ‘.encode’ suggestions I saw online were working either. Just changing the shell env with your command was simple and did the trick.