Why

I have been procrastinating getting my first post up here. I have also been feeling a bit of imposter syndrome surrounding some of the metaphorical plates I have spinning. The solution I came up with is this:

  1. Going back and re-learning some of the basics
  2. Sprinkling in a few topics I wish I knew more about
  3. Documenting the process here on this website I was not sure what to do for this post, so I leaned on ChatGPT and Llama 3.2 to give me a bit of inspiration. I asked for a python challenge that I could complete in an hour. It told me to make a guessing game. This post covers my thought process and what I came up with.

Planning

First I listed what I needed the script to do.

  • Generate and save random number
  • Ask a user for a input validate the input
  • Create a loop that :
    • Gets user input from the player
    • Validates the user input
      • Handles errors if input invalid
    • Compares the guess to the variable
      • Breaks the loop if guessed correctly

rng Function

I was not sure how this would go, but I just opened up my editor and started typing. This was the first code I tried to run:

answer = 0  # Will store the random number
spread = 10 # Will store range for guessing

def rng(x):
    global answer
    answer = random.randint(1, x)
    print(answer)  # comment out later

rng(spread)

Immediately, when I saw 'random' is not defined in the output, I realized I needed to import the random library. After doing so my random number generating function was working. I then made it so this random number gets set to the answer variable:

validGeuss Function

Next I wanted to make and test a function to validate what a user inputs.

import random

answer = 0  # Will store the random number
spread = 10 # Will store range for guessing
guess = 0 # Will store guess

def rng(x):
    global answer
    answer = random.randint(1, x)
    print(answer)  # comment out later

rng(spread)

def validGuess(y):
    if 1 <= y <= spread:
        return True
    else:
        return False

guess = int(input(f"Enter a guess between 1 and {spread}, then press enter: "))
isValid = validGuess(guess)
print("isValid = " + str(isValid))

isCorrect Function

The last thing I needed to do was add a function that checks if the guess is correct, and prints the result of the game. I also tidied things up a bit, and added some dialogue. This is what I was left with.

import random

answer = 0  # Will store the random number
spread = 10 # Will store range for guessing
guess = 0 # Will store guess

def rng(x):
    global answer
    answer = random.randint(1, x)
    # print(answer)  # comment out later

def validGuess(y):
    if 1 <= y <= spread:
        return True
    else:
        return False

def isCorrect(guess):
    if guess == answer:
        print("Correct!")
    else:
        print(f"Wrong! The right answer was {answer}.")

print("Welcome to pyGuesser, a guessing game written in python.")
rng(spread)
guess = int(input(f"Enter a guess between 1 and {spread}, then press enter: "))
isValid = validGuess(guess)
# print("isValid = " + str(isValid))
if isValid == False:
    print(f"Try again, enter something between 1 and {spread}.")
else:
    isCorrect(guess)

Gameplay

PS C:\Users\home\Documents\GitHub\python-playground> python .\guesser.py
Welcome to pyGuesser, a guessing game written in python.
Enter a guess between 1 and 10, then press enter: 3
Wrong! The right answer was 10.
PS C:\Users\home\Documents\GitHub\python-playground> python .\guesser.py
Welcome to pyGuesser, a guessing game written in python.
Enter a guess between 1 and 10, then press enter: 4
Wrong! The right answer was 7.

Conclusion

To be honest, this was not very challenging. I am hoping that showing myself this will result in me posting more. I am just going to try and do a little better each time.

Until then.

Ben :)