'Where / How to add another input validation? Multiplication Game

At 53 years old, I've decided to teach myself some Python for fun. I've been using Kylie Ying's YouTube video, "12 Beginner Projects" as a jumping off point and many other sources as well.

I've been trying to write a simple program to help my son learn his multiplication tables with moderate success. I found a basic game online that did not allow for the selection of the times table. I modified the program to allow the user to select the times table. Occasionally, my son would break the program with a typo. I then made my first foray into input validation. I've reached a point where if the user types a letter instead of a number, the program loops back and allows another input.

I would like to add one last level of input validation. At present, when selecting a times table, the user is asked to input a number between 2 & 12. At present the program accepts any value that is a number. I'm using .isdigit() to validate the input is a number. After validating the input is a number, I want to validate the number is between 2 & 12. I've tried using "if" statements and while loops. I end up with string errors or "int" errors.

Will someone please look at my little program and suggest where and how to restrict the user from inputting any number less than 2 or greater than 12 when selecting a multiplication table? Thank you in advance

import random

print ('Welcome to the multiplication game.')
print ('How well do you know your 2-12 multiplication tables?')
print ('Which times table would you like to use?')

#user selects the times table

flag = True

while flag:
    number1 = (input('Select a number between 2-12 '))

    if number1.isdigit():
        print('You will be working on the ',number1, ' times tables')
        number1 = int(number1) #changes input into integer(numerical) input
        flag = False
    else:
        print(number1, ' is not a valid number. Please select a number between 2-12.')

#"for' loop sets question count to 20 / indent after 'for' loop
for num in range(0,20):

    number2 = random.randint(2,12)
    answer = number1 * number2

    # user inputs their "guess"
    #if input during int(input) causes ValueError, 'except ValueError' deals with
    #the error

    while True:
        try:
            guess = int(input(f'What is {number1} x {number2}? '))
        except ValueError:
            print('Sorry, I don\'t understand that')
            continue
        else:
            break

    # program compares 'guess' to 'answer'. If answer calls up 'ValueError', 'except ValueError'
    #deals with error

    print (f'What is {number1} x {number2}? ')
    while guess != answer:
        try:
            guess = int(input(f'What is {number1} x {number2}? '))
        except ValueError:
            print('Sorry, I don\'t understand that')
        if guess != answer:
            print ('No, Try again')

    print ("You got it!")

print ("That's it, good work!")


Solution 1:[1]

This statement:

if number1.isdigit():

can be turned into

if number1.isdigit() and 2 <= int(number1) <= 12:

The other answer uses a nested-if statement, which does work. That being said, you generally want to avoid additional nesting when possible (makes your code harder to read and/or refactor), and you generally want to avoid repeating code (for similar reasons). This answer avoids both of these issues.

Solution 2:[2]

Change this section of code here:

while flag:
    number1 = (input('Select a number between 2-12 '))

    if number1.isdigit():
        if int(number) >= 2 and int(number) <= 12:
            print('You will be working on the ',number1, ' times tables')
            number1 = int(number1) #changes input into integer(numerical) input
            flag = False
        else:
            print(number1, ' is not a number between 2 and 12. Please select a number between 2-12.')
    else:
        print(number1, ' is not a valid number. Please select a number between 2-12.')

All this does is check if the number is less than or equal to 12 and greater than or equal to 2, and, if not, loops back, the same as if your son put in a non-digit input. If you want less lines of code, use BrokenBenchmarks answer. This one however tells what you did wrong.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BrokenBenchmark
Solution 2