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
"""
Make a program in which the computer randomly chooses a number between 1 to 10,
1 to 100, or any range.
"""
def intro():
print(f"Welcome to our number guessing game.")
print(f"We'll ask you to pick a range of numbers from 0 up to 100."
f"Then you'll get some chances to guess the number. "
f"The number of guesses you get will scale based on the range you "
f"choose. From 0 - 50, you'll get 5 chances. From 0 - 100, you'll"
f"get 10 chances. Good luck!")
print("Choose a range: ")
print("1. 0 - 50")
print("2. 0 - 100\n")
num_guesses = {
"1": (5, 50),
"2": (10, 100)
}
num_range = input()
while True:
if num_range == "1" or num_range == "2":
return num_guesses[num_range]
else:
print(f"{num_range} is not a valid selection. "
f"Please select either 1 or 2.")
num_range = input()
def guess_number(num_guesses, num_range):
random_number = random.randint(0, num_range)
guess_count = 0
print(f"Random num is: {random_number}")
while guess_count < num_guesses:
guess = int(input(f"Pick a random number between 0 and "
f"{num_range}: \n"))
if guess == random_number:
print("CONGRATS, YOU WIN!")
break
else:
guess_count += 1
print(f"Incorrect, try again! You have "
f"{num_guesses - guess_count} guess(es) left.\n")
else:
print("Womp womp, you lose :(.")
if __name__ == "__main__":
num_guesses, num_range = intro()
guess_number(num_guesses, num_range)
guess_number(),print(f"Random num is: {random_number}")comes before thewhileloop. Doesn't that tell the user what the number is before they make any guesses? It should probably go at the end withprint("Womp womp, you lose :(."). \$\endgroup\$