2
\$\begingroup\$

I am so new to programming. Trying to learn as I go. I made a very simple "Guess the number" game. Just wanted to get some critiques, see if I could clean it up or change anything.

# user guesses the number. Returns Yes or sorry.
import random

# Instruct user to guess a number.
number = int(input("Guess a number, see if you're right."))

rn = random.randint(1,10)

if number == rn:  # User number equals random number
    print("You got it.")

else:  # Users number is not the same as random number.
    print("sorry")
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Shouldn't be there a loop, and hints, to give the player more than just one shot? \$\endgroup\$ Commented Apr 2, 2018 at 15:48

2 Answers 2

3
\$\begingroup\$

use good variable names: If you use randomNumberinstead of rn and userNumber instead of number then the 'if'-statement changes to

if randomNumber == userNumber :

and your comment in this line becomes obsolete.

The current version of your program can be rewritten without any variables at all:

import random
if 1<=int(input("Guess a number, see if you're right!"))<=10:
  if random.randint(1,10) == 1:  # User succeeds, propability=1/10
      print("You got it.")
  else:  # user fails, propability=9/10
      print("Sorry.")
else: # this input number cannot succeed
    print("Sorry.")

use comments wisely: Don't comment to much. If you change your code then you have to change your comments, too. Otherwise the comments are contradictory to the code. So your comment in the first lines says that 'Yes' is a possible answer of your program, but this is not true.

A comment like

# Instruct user to guess a number.

is not useful. The purpose of the statement it described is immediate clear.


docstrings: The text in the first line of the module that describes the module (docstring) should be a '''-string and not a #-comment.


user interface:

An input text like

"Input an integer number between 1 and 10 and press <return>!"

would be more useful.

If the user enters a value that is not a number, e.g. the string 'a', an error message like

Traceback (most recent call last):
  File "python", line 5, in <module>
ValueError: invalid literal for int() with base 10: 'a'

is printed. This is not very user friendly. Maybe it is also useful to warn a user if he enters an integer less than 1 or greater than 10.

very pedantic: In accordance to the other output you print, if a user fails you should print Sorry. instead of sorry. Sorry! may me even better. The exclamation mark may be also appropriate here:

Guess a number, see if you're right!

Maybe you should ask he user again for input if invalid input was entered.


don't use Magic Numbers: Use something like

MAX_NUMBER=10
....
random_number=random(1, MAX_NUMBER)

Also other occurrences of 10 should be replaced by MAX_NUMBER, e.g. in the input text.


format: Placing an empty line after almost every line of code blows up the code but does not make it more readable. Use empty line to structure the code. So I think it makes no sense to separate the 'if' and the 'else'-block of an 'if'-statement by an empty line, because they belong together.

\$\endgroup\$
2
\$\begingroup\$

You could add a "while" loop instead of an "if" statement. This will allow the user to keep trying until it is correct, and is very pythonic.

Also if you want to be really clean review PEP8 "function and variable names". You'll want your variables lower case, with underscores "_". Python Zen says "Explicit is better than implicit." Therefore I would change around your variable names to be more readable.

For example:

import random

max_num = 10
random_num = random.randint(0, max_num)

print("Guess a number:")
guess = int(input())

while not guess == random_num:
    print("Sorry, try again:")
    guess = int(input())
print("You got it!")

I would also suggest adding a try-except block around the input() call to prevent the code from crashing if the user inputs anything other than an integer. In Python int("a string") will return a ValueError. So you would want to provide for this potential exception - alert the user accordingly and allow them to try again.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ the purpose of a code review is no to write a new program but to reveiew the code that the OP posted. \$\endgroup\$
    – miracle173
    Commented Apr 4, 2018 at 8:42
  • \$\begingroup\$ It's a short answer, but it looks like a review to me... it provides feedback on an issue that is now improved. \$\endgroup\$
    – rolfl
    Commented May 4, 2018 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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