Random number generator [Beginner] I'm learning Python and I'm building a basic random numbr generator. The code below works as expected. I'm just wondering if it uses best practice / can be made better.
Some future considerations might be adding a way for users to pick a random that isn't limited to 0-50 or 0-100, and finding a way to scale the number of guesses dynamically based on their choice.
import random

&quot;&quot;&quot;
Make a program in which the computer randomly chooses a number between 1 to 10,
1 to 100, or any range.
&quot;&quot;&quot;


def intro():
    print(f&quot;Welcome to our number guessing game.&quot;)
    print(f&quot;We'll ask you to pick a range of numbers from 0 up to 100.&quot;
          f&quot;Then you'll get some chances to guess the number. &quot;
          f&quot;The number of guesses you get will scale based on the range you &quot;
          f&quot;choose. From 0 - 50, you'll get 5 chances. From 0 - 100, you'll&quot;
          f&quot;get 10 chances. Good luck!&quot;)
    print(&quot;Choose a range: &quot;)
    print(&quot;1. 0 - 50&quot;)
    print(&quot;2. 0 - 100\n&quot;)
    num_guesses = {
        &quot;1&quot;: (5, 50),
        &quot;2&quot;: (10, 100)
    }
    num_range = input()
    while True:
        if num_range == &quot;1&quot; or num_range == &quot;2&quot;:
            return num_guesses[num_range]
        else:
            print(f&quot;{num_range} is not a valid selection. &quot;
                  f&quot;Please select either 1 or 2.&quot;)
            num_range = input()


def guess_number(num_guesses, num_range):
    random_number = random.randint(0, num_range)
    guess_count = 0
    print(f&quot;Random num is: {random_number}&quot;)
    while guess_count &lt; num_guesses:
        guess = int(input(f&quot;Pick a random number between 0 and &quot;
                          f&quot;{num_range}: \n&quot;))
        if guess == random_number:
            print(&quot;CONGRATS, YOU WIN!&quot;)
            break
        else:
            guess_count += 1
            print(f&quot;Incorrect, try again! You have &quot;
                  f&quot;{num_guesses - guess_count} guess(es) left.\n&quot;)
    else:
        print(&quot;Womp womp, you lose :(.&quot;)


if __name__ == &quot;__main__&quot;:
    num_guesses, num_range = intro()
    guess_number(num_guesses, num_range)
First, an issue with behaviour: In guess_number you don't make sure the input from the user is a number at all. So you might end up calling int(&quot;oops&quot;). This raises a ValueError, and since there's nothing to catch that, the program crashes. You'll want to do something about that. A simple solution could be
try:
    guess = int(input(...))
    if guess == random_number:
        ...
    else
        ...
except ValueError:
    print(&quot;That doesn't look like a number to me, but that's OK, I'll count it anyway :)&quot;)
    guess_count += 1

Side note, guesses that aren't in the interval also add to the counter. That might be on purpose, but since the validity of the input wasn't checked at all I don't want to assume, so I'm pointing it out.
There are also a couple minor issues with intro
First off, you're duplicating the num_range = input() line. If you get the input at the start of the loop (before the if) instead it'll be easier to change if for some reason you need to
Second, in python the usual approach is that it's better to ask for forgiveness than permission. That if check would usually be written using a try/except KeyError block (a bit like above). But if you don't want to do that, a more robust approach than the current one could be to check if num_range in num_guesses, in case a new option is added, or the program changes from accepting 1 and 2 to accepting &quot;a&quot; and &quot;b&quot;.
On a related third note, the messages don't actually know what options are available, so there's a risk they get out of sync with the rest of the program. You can get a list of valid options as num_guesses.keys(), or use a for loop to iterate over the keys in num_guesses directly
Finally, I don't love the variable names. num_guesses sounds like it should be the number of guesses, but it isn't a number at all. Similarly, num_range doesn't have much to do with ranges of numbers either. num_range feels like it would be better described by a name like selection or choice, and num_guesses is a collection of available options, so maybe it could be called options?
options = {
    &quot;1&quot;: (5, 50),
    &quot;2&quot;: (10, 100)
}

print(&quot;Welcome to ... on the range you choose.&quot;)

for option in options:
    print(f&quot;From 0 - {options[option][1]}, you'll get {options[option][0]} chances.&quot;)

while True:
    print(&quot;Choose a range:&quot;)
    for option in options:
        print(f&quot;{option}. 0 - {options[option][1]}&quot;)

    selection = input()
    try:
        return options[selection]
    except KeyError:
        print(f&quot;{selection} is not a valid selection.&quot;)