patternpythonModerate
Guess the randomly generated number game
Viewed 0 times
generatedthenumberrandomlygameguess
Problem
I've been working on this for the last few days. Any improvement ideas? (Notes are very simple, making this for school and notes have to be for complete novices to Python).
```
import random # Allows the computer to generate random numbers, vital for the program to function correctly.
import sys # Allows a new selection of system related commands to be used; I use sys.exit() to close the program.
def oneToTen():
ten = random.randint(1,10) #Generates a random integer between 1 and 10.
attempts = 1
guess = int(input("Make your guess!"))
while guess != ten: #Everything indented below this will happen when the user's guess is NOT equal to the random number. != means not equal to.
if guess ten: #This will happen when the user's guess is higher than the randomly generated number.
print("Lower!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
if guess > 10: #This will happen when the user's guess is larger than 10.
print("Enter a valid number!")
guess = int(input("Make your guess!"))
elif guess == ten: #This will happen when the user's guess is equal to the random number.
print("Well done! You guessed it in",attempts,"tries!")
end = input()
sys.exit() #Exits the program.
elif attempts == 5:
print("The number was",ten,"!")
end = input("Maximum amount of tries reached!")
sys.exit()
def oneToTwenty():
twenty = random.randint(1,20)
attempts = 1
guess = int(input("Make your guess!"))
while guess != twenty:
if guess twenty:
print("Lower!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
```
import random # Allows the computer to generate random numbers, vital for the program to function correctly.
import sys # Allows a new selection of system related commands to be used; I use sys.exit() to close the program.
def oneToTen():
ten = random.randint(1,10) #Generates a random integer between 1 and 10.
attempts = 1
guess = int(input("Make your guess!"))
while guess != ten: #Everything indented below this will happen when the user's guess is NOT equal to the random number. != means not equal to.
if guess ten: #This will happen when the user's guess is higher than the randomly generated number.
print("Lower!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
if guess > 10: #This will happen when the user's guess is larger than 10.
print("Enter a valid number!")
guess = int(input("Make your guess!"))
elif guess == ten: #This will happen when the user's guess is equal to the random number.
print("Well done! You guessed it in",attempts,"tries!")
end = input()
sys.exit() #Exits the program.
elif attempts == 5:
print("The number was",ten,"!")
end = input("Maximum amount of tries reached!")
sys.exit()
def oneToTwenty():
twenty = random.randint(1,20)
attempts = 1
guess = int(input("Make your guess!"))
while guess != twenty:
if guess twenty:
print("Lower!")
attempts = attempts + 1
guess = int(input("Make your guess!"))
Solution
Some general comments:
-
Your comments need some work. For example:
This is pretty much the anti-pattern for comments. Comments should explain why the code works this way, not repeat information that I could learn by reading the code.
Most of your comments are redundant or unnecessary, and can be removed. In particular, don’t explain why you’re importing modules unless it’s particularly unusual – this just adds visual noise, and is prone to become out-of-date.
- The Python style guide recommends that lines should be wrapped to 79 characters; comments or docstrings to 72. A lot of your comments disappear of the side of the screen in my editor. Try to make your lines shorter.
- Most Python variable names are
snake_case, notcamelCase, with the exception of class names, which arePascalCase.
- Your variable names are pretty obtuse. Using
tenorhundredfor random integers makes the program much harder to read, because the name sounds like these are constants, not random values.
-
Your comments need some work. For example:
attempts = attempts + 1
#Adds one to the number of attempts the user has taken to guess the number.This is pretty much the anti-pattern for comments. Comments should explain why the code works this way, not repeat information that I could learn by reading the code.
Most of your comments are redundant or unnecessary, and can be removed. In particular, don’t explain why you’re importing modules unless it’s particularly unusual – this just adds visual noise, and is prone to become out-of-date.
- Your three functions
oneToTen(),oneToTwenty()andoneToFifty()are all very similary. It would be better to have a singleguess_number(max_number)function which asks the user to guess a number between 1 and max_number, and just call this with different parameters as appropriate, than to have all the code copying.
- Using
sys.exit()is a terrible way to yield control flow from a function, because it kills the entire script. It’s much better to usereturn, in case the rest of the program needs to continue.
- If I give your program totally invalid input, it says nothing and just exits silently. It should warn me if I’ve used an inappropriate choice.
Code Snippets
attempts = attempts + 1
#Adds one to the number of attempts the user has taken to guess the number.Context
StackExchange Code Review Q#95724, answer score: 15
Revisions (0)
No revisions yet.