use good variable names: If you use randomNumber
instead 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.