-2

I have an input in a function, where it requires to print a dictionary and actually choose the number you want (based on your choice) however its not working for me.

def add_item():
        item = str(input("Gender>  "))
        types = int(input(print_nationality(), "Choice >"))
...


def print_nationality():
    dict = {
        1: "English",
        2: "American",
        3: "French",
        4: "Spanish"
    }
    for i in range(len(dict)):
        print(dict[i])

gooflies
  • 9
  • 3
  • 1
    Does this answer your question? [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – Equinox Oct 31 '20 at 17:23

3 Answers3

1

it works fine for me, the only modification I made is to discard 0 and start from 1 (and for good practice renamed the variable to name that is not type, well not recommended):

def print_nationality():
    m_dict = {
        1: "English",
        2: "American",
        3: "French",
        4: "Spanish"
    }
    for i in range(1,len(m_dict)+1):
        print(m_dict [i])
        
print_nationality()

output:

English
American
French
Spanish
Yossi Levi
  • 1,258
  • 1
  • 4
  • 7
0

You loop over the dictionary values (not over the index of the size of dictionary) and you have to call print_nationality() before and not inside the input(..) command:

def print_nationality():
    menu = {
        1: "English",
        2: "American",
        3: "French",
        4: "Spanish"
    }

    # directly iterate the keys of the dict
    for i in menu:
        print(f"{i}: {menu[i]}")

def add_item():
    item = str(input("Gender>  "))
    # print first, then ask for input
    print_nationality()        
    types = int(input("Choice >"))

should do it. Never name variables after built ins, your dict = .... hides the dict(..) built in.

Output:

Gender>  whatever
1: English
2: American
3: French
4: Spanish
Choice >
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Yes but I want my answer to be saved in the equivalent record. if for example the user chooses 1, my record will be saved as English. – gooflies Oct 31 '20 at 17:33
  • @goo There is no code that does anything with records. Where do you store them? and how? Thats not really part of the question, is it? – Patrick Artner Oct 31 '20 at 17:35
  • I store them in a local db, entries are being stored as numbers, I would like them to be stored as equivalent strings. – gooflies Oct 31 '20 at 17:42
  • @goo does your databasetablecolumn allow to store strings? if it currently is a number you can not put a string inside. if you want to get the stringified input, you can do so using the number you aquired and asking the dictionary again : print( menu[2] ) will print American. – Patrick Artner Oct 31 '20 at 17:45
  • ok will try that, thank you. this will be done in another function ? – gooflies Oct 31 '20 at 17:51
0

The correct way of iterating through a dictionary in python would be:

for key, item in m_dict.items():

The m_dict.items() will give you the possibility to use the key and the item in the for loop. In your case you could use it like this:

def print_nationality():
    dict = {
        1: "English",
        2: "American",
        3: "French",
        4: "Spanish"
    }
    for key, item in m_dict.items():
        print(item)

Source: Iterating over dictionaries using 'for' loops

LarsZauberer
  • 53
  • 2
  • 6