Answer:
import random
from time import sleep as sleep
def main():
# Initialize random seed
random.seed()
# How many guesses the user has currently used
guesses = 0
# Declare minimum value to generate
minNumber = 1
# Declare maximum value to generate
maxNumber = 20
# Declare wait time variable
EventWaitTime = 10 # (Seconds)
# Incorrect config check
if (minNumber > maxNumber or maxNumber < minNumber):
print("Incorrect config values! minNumber is greater than maxNumber or maxNumber is smaller than minNumber!\\DEBUG INFO:\\minNumber = " + str(minNumber) + "\\maxNumber = " + str(maxNumber) + "\\Exiting in " + str(EventWaitTime) + " seconds...")
sleep(EventWaitTime)
exit(0)
# Generate Random Number
secretNumber = random.randint(minNumber, maxNumber)
# Declare user guess variable
userGuess = None
# Ask for name and get input
name = str(input("Hello! What is your name?\\"))
# Run this repeatedly until we want to stop (break)
while (True):
# Prompt user for input then ensure they put in an integer or something that can be interpreted as an integer
try:
userGuess = int(input("Guess a number between " + str(minNumber) + " and " + str(maxNumber) + ": "))
except ValueError:
print("ERROR: You didn't input a number! Please input a number!")
continue
# Increment guesses by 1
guesses += 1
# Check if number is lower, equal too, or higher
if (userGuess < secretNumber):
print("You guessed: " + str(userGuess) + ". Too low.\\")
continue
elif (userGuess == secretNumber):
break
elif (userGuess > secretNumber):
print("You guessed: " + str(userGuess) + ". Too high.\\")
continue
# This only runs when we use the 'break' statement to get out of the infinite true loop. So, print congrats, wait 5 seconds, exit.
print("Congratulations, " + name + "! You beat the game!\\The number was: " + str(secretNumber) + ".\\You beat the game in " + str(guesses) + " guesses!\\\\Think you can do better? Re-open the program!\\(Auto-exiting in 5 seconds)")
sleep(EventWaitTime)
exit(0)
if __name__ == '__main__':
main()
Step-by-step explanation: