A dictionary with numeric keys

A dictionary with numeric values

It’s okay to have a dictionary whose values are numbers:

numericvalues.py

Please type the word for a number from one to ten inclusive: six
The word six is the number 6.

Please type the word for a number from one to ten inclusive: four
The word four is the number 4.

Please type the word for a number from one to ten inclusive: zero
Sorry, "zero" is not a number in the range one to ten.

Please type the word for a number from one to ten inclusive:

A dictionary with numeric keys

But it’s unnecessarily complicated to have a dictionary whose keys are consecutive integers:

numerickeys1.py

Please type an integer from 1 to 10 inclusive: 6
The word for 6 is "six".

Please type an integer from 1 to 10 inclusive: 4
The word for 4 is "four".

Please type an integer from 1 to 10 inclusive: hello
Sorry, "hello" is not an integer.
Try again.

Please type an integer from 1 to 10 inclusive: 0
Sorry, 0 is not an integer in the range 1 to 10.
Try again.

Please type an integer from 1 to 10 inclusive:

A list

A list is simpler than a dictionary whose keys are consecutive integers.

numerickeys2.py

Please type an integer from 1 to 10 inclusive: 6
The word for 6 is "six".

Please type an integer from 1 to 10 inclusive: 4
The word for 4 is "four".

Please type an integer from 1 to 10 inclusive: hello
Sorry, "hello" is not an integer.
Try again.

Please type an integer from 1 to 10 inclusive: 0
Sorry, 0 is not an integer in the range 1 to 10.
Try again.

Please type an integer from 1 to 10 inclusive:

Things to try

  1. Don’t hardcode the number 10 into lines 25 and 42 of numerickeys2.py. For example, you could create the following variable at line 22.
    maximum = len(words) - 1
    
    Then change line 25 from
            s = input("Please type an integer from 1 to 10 inclusive: ")
    
    to
            s = input(f"Please type an integer from 1 to {maximum} inclusive: ")