Aug-14-2019, 09:54 AM
Hey all, I'm pretty new to Python, well new might be an understatement.
I've been tasked to create one of those guess the number games. One of my friends was nice enough to let me use his code as a base. All I'm capable of is rephrasing some print messages, comments, variables, and fixing some really basic problems. But being as green as grass has really made this a struggle for me.
I seem to have broken the code, as it's now getting errors.
I'd like to ask for help from anyone, any and all help is appreciated.
Thanks in advance.
The code:
I've been tasked to create one of those guess the number games. One of my friends was nice enough to let me use his code as a base. All I'm capable of is rephrasing some print messages, comments, variables, and fixing some really basic problems. But being as green as grass has really made this a struggle for me.

I seem to have broken the code, as it's now getting errors.

I'd like to ask for help from anyone, any and all help is appreciated.
Thanks in advance.
The code:
import random #Brings in Python's built-in random module. #This variable is currently set to false. It determines whether the player has won or not. When this is true, the game has ended and the player has won. player_win == False #The set of code below begins the program by asking what the player's name is. while player_win == False: print("Hello, and welcome to the program!") player_name = input("Please enter your name to continue! \n What is your name? \n :") #This variable will store what the player has entered when they are asked to input a name. if player_name.isalpha(): #This code makes it so that players can only enter alphabetic characters as their name. break #After the player has entered their name, it will greet them using the name they entered. print("Hey {}, let's get started!".format(player_name)) print("I'm thinking of a number between 1 - 20, can you guess what it is? \n Be careful though, because you only have 7 lives! \n Oh, and I'll give you clues by telling you if the number is higher or lower than your guess!") invalid_response = False #This is a variable that checks whether or not the player has entered an invalid response. lives = 7 #This variable sets the amount of guesses the player is allowed before losing the game. random_integer = random.randrange (1, 20) #This generates a random number between 1 - 20, which the program will store as the number it is 'thinking' of. while lives > -1: #If somehow the player manages to get to -1 lives this will create an infinite loop. But this shouldn't be possible, so this will be used to indicate that the player has lost the game. try: if player_win == True: #If the player has won this code will be triggered. play_again = input("Good game, wanna play again? \n") invalid_values = True if play_again == "yes": print("Here we go again!") #The code below restarts the game by repeating some of the code, as if the program were start for the first time. player_win = False lives = 7 random_integer = random.randrange (1, 20) elif play_again == "no": break else: print("""Please enter a valid response either "yes" or "no" """) #When lives hit zero, this code will be triggered and tell the player what number the program was 'thinking' of elif lives == 0: print("Aw man, it looks like you're out of lives! The number I was thinking of was {}".format(random_integer)) player_win = True #This chunk of code here asks the user to guess what number the program is 'thinking of' if player_win == False lives -= 1 player_guess = int(input("What number am I thinking of? \n") #The chunk of code below checks if the players response is invalid. If it is the program will ask them to enter a valid response. invalid_value = False except SyntaxError and ValueError print("Please enter a whole number/integer. \n Enter the number now.") player_guess = False invalid_value = True lives += 1 if player_guess == random_integer and invalid_response == False: # this code tells the player whether the number they guessed is either to high or too low. print("You guessed the correct number!") player_win = True #This code tells the player that their guess is higher than the number the program is 'thinking' of. elif player_guess > random_integer and player_guess < 20 and player_win == False and invalid_value == False: print("Looks like your guess is higher than my number!") #This code tells the player that their guess is lower than the number the program is 'thinking' of. elif player_guess < random_integer and player_win == False and invalid_value == False: print("Your guess is lower than my number!") #The code below reminds the player the range of numbers the program can 'think' of. elif player_guess >= 20 or player_guess <= 0 and player_win == False and invalid_value == False : print("Remember that the number I'm thinking of is between 1 and 20.")