It’s okay to have a
dict
ionary
whose values are numbers:
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:
But it’s unnecessarily complicated to have a
dict
ionary
whose keys are consecutive
int
egers:
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
is simpler than a
dict
ionary
whose keys are consecutive
int
egers.
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:
numerickeys2.py
.
For example,
you could create the following variable at line 22.
maximum = len(words) - 1Then 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: ")