3
\$\begingroup\$

I made a really basic number guessing game based off a list of recommended projects, and i want to see if it follows the normal coding conventions before I aim to improve it(with lives, or hints, ect). This is also my first attempt at loops. I am wondering if this follows the normal coding conventions, if i was unnecessarily repetitive with the code, and if there is any thing I can do to improve the code.

#Guessing Game!
#The code has the user guess a number from 1-100
#if they get it right, they are congratulated and asked to play again
#if they get it wrong, they must keep guessing 

#Game code
import random as r
play = True

#rnum = The random number
#gnum = The guessed number
while play:
    x = r.randrange(1,100,1)
    rnum = str(x)
    
    #this exists for testing
    print(rnum)
    
    gnum = input("Guess the number!")
    if gnum == rnum:
        print("Good Job! Play Again?")
        play_again = input("y/n")
        if play_again == "y":
            continue
        else:
            break

    if gnum != rnum:
        gnum = input("Nope, guess again!")
        if gnum == rnum:
            print("Good Job! Play Again?")
            play_again = input("y/n")
            if play_again == "y":
                continue
            else:
                break

print("Thank you for playing!")
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Broadly: whereas the game does work, it isn't very fun, since no feedback is given to the user (e.g. that the number guessed is too high or too low, or that you're close or far). The only way to win is to try every single number from 1 to 100.

Your play loop variable can be deleted since you can break out of the loop directly when needed.

r is not a very good alias for the random module. Either keep it as random, or use import from syntax.

randrange is not the right function to call; since you care about an inclusive range call randint instead. As it stands, your code doesn't do what you say it does since the maximum will only be 99, not 100.

You have repeated code that should be centralised - your "play again" section.

Rather than casting your rnum as a string, you should do the opposite and validate and cast the user input to an integer.

Suggested

"""
Guessing Game!
The code has the user guess a number from 1-100
if they get it right, they are congratulated and asked to play again
if they get it wrong, they must keep guessing
"""

from random import randint


while True:
    # rnum = The random number
    rnum = randint(1, 100)

    while True:
        # gnum = The guessed number
        gnum = input("Guess the number! ")
        if not gnum.isnumeric():
            print("Invalid integer")
        elif int(gnum) == rnum:
            break
        else:
            print("Nope, guess again!")

    print("Good Job! Play Again?")
    play_again = input("y/n")
    if play_again != "y":
        break


print("Thank you for playing!")
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.