In the past few days i have started to learn python 3 in my spare time and i am really enjoying it so far.
I have attempted many simple programs such as a palindrome checker and reverse a string in an attempt to improve my skills.
I was hoping that someone would be able to provide some feedback on the following program which is a guessing game program. The user is able to choose the number of guesses and the max possible value.
From my own perspective i think that possibly i am relying too much on while loops and if/else statements. Also i am not sure if my comments are sufficient, do they lack detail? Also i use parentheses around the conditionals in my if/else statements and in my while loops as i find it easier to read the condition this way. Is this a bad idea? Could it confuse people who are trying to read my code?
# In this program, the user must try to guess a random number generated by the computer
from random import randint
def guess_game():
replay = 1
#Replay allows a user to replay the game without having to call the function again.
#The values are: 1 = Replay, any other character = End program
while (replay == 1):
#lives = Number of chances you have to correctly guess the answer before the program finishes
lives = int( input("How many lives would you like? ") )
#max_value = upper bound for the randomly generated number. Starts at 0
max_value = int( input("Please choose the largest number that can be generated: ") )
random = randint(0, max_value) #Generates a random int between 0 and max_value
num_of_guesses = 1 #keeps track of the number of guesses made by the user
while (num_of_guesses <= lives):
guess = int( input("Please enter your guess: ") )
if (guess == random):
print("Good guess, that is correct! You got the right answer on guess number", num_of_guesses, ", well done!\n")
break #if answer is correct this will exit the while loop
else:
print("Unlucky, that guess was incorrect. You have", (lives-num_of_guesses), "lives remaining.\n")
num_of_guesses = num_of_guesses + 1
if (num_of_guesses > lives):
print("You have run out of guesses. The correct answer was:", random)
replay = int( input("\nWould you like to play again? Enter 1 for yes, any other character for no: ") )
if (replay != 1):
print("Thank you for playing. See you next time")
guess_game()
for
loop. in your case,for num_of_guesses in range(1, lives + 1):
\$\endgroup\$random
. instead, something descriptive of its purpose, maybevalue_to_guess
. \$\endgroup\$replay
part in another function. it is not strictly part of the game. \$\endgroup\$