Search code examples
python-3.7

Dictionary ordered keys


grades = {}
while True:
    k = input('enter input ')
    val = input('enter input ')
    grades[k] = val

In this scenario I want k to be a number that is given in order, not asked for by the user.

This will give the result of

grades = {1:'user input first', 2:'user input second') this repeats for all user inputs

Solution

  • How about:

    grades = {}
    k = 1
    while True:
        val = input('enter input ')
        grades[str(k)] = val
        k += 1