Why do I get a "u" in front of my menu item when I have the user input the entries?


#1

I'm working on 11.New Entries of the Python Lists and Dictionaries lesson. I am using the following code to have the user dynamically add new items to the menu dictionary.

menu = {} # Empty dictionary
menu['Chicken Alfredo'] = 14.50 # Adding new key-value pair
print menu['Chicken Alfredo']

Your code here: Add some dish-price pairs to menu!

def add_dish():
# Have the user add a new dish to the menu dictionary
food = raw_input("What dish do you want to add to the menu? Input END to exit. ")
if food.lower() == "end": # If the user inputs "End" exit the loop
return ""
else:
cost = input("How much does the dish cost? ")
menu[food] = cost
print food
add_dish() # loop the function

add_dish()

print "There are " + str(len(menu)) + " items on the menu."
print menu

When I use this code, all of the new menu items have the letter 'u' appended to the beginning of the dish. For example, 'Bread' is listed as u'Bread', and 'Ravioli' is listed as u'Ravioli' See the following example from when I ran the code:

14.5
What dish do you want to add to the menu? Input END to exit. Bread
How much does the dish cost? 5
Bread
What dish do you want to add to the menu? Input END to exit. Ravioli
How much does the dish cost? 23.25
Ravioli
What dish do you want to add to the menu? Input END to exit. End
There are 3 items on the menu.
{'Chicken Alfredo': 14.5, u'Ravioli': 23.25, u'Bread': 5}
None

Does anybody know why this is happening and why the dictionary isn't listed as
{'Chicken Alfredo': 14.5, 'Ravioli': 23.25, 'Bread': 5}

Thanks!


#2

You also get apostrophes (') around the string and curly brackets ({}) around your dict, and some spaces and commas, those aren't part of the data either. The u means that it is a unicode string.

If you want it printed differently you'll have to write code that does that.


#3

ionatan, thanks for the explanation. I changed the following line of code, and it eliminates the extra "u" from the menu dictionary entries.

food = str(raw_input("What dish do you want to add to the menu? Input END to exit. "))


#4

What if your menu contains items with non-ascii characters?

I'd like some شاي, please.


#5

Hmm. Well beyond the scope of my knowledge at this point. Do they cover non-ascii characters in the graduate course?


#6

@abigail854

Just use one of the built-ins to accomplish some fun stuff.

import re

menu = ['apple', 'orange']

def take_order():
    take_ = raw_input("What would you like to order?")
    if take_ not in menu and re.match(r"[a-z]", take_):
        return 'Squid'
    else:
        return "You ordered: {}".format(take_)

You can build just about anything you want, quickly and easily with out that much of an issue.

EDIT:

I should not that the re module stands for regular expressions, and it is super helpful.


#7

Each thing you do in your code should have a purpose.

Downgrading from unicode to str has no purpose! It's just worse.

When you present something to an end-user, you would show them the data, not the datatypes, and unicode can represent the same data as str, and more, so it's just better.