I'm currently doing a python excersise from learnpython.org: I'm having trouble with the bolded extra excersise:
Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)
Extras:
Keep the game going until the user types “exit”
Keep track of how many guesses the user has taken, and when the game ends, print this out.
Here is my code so far:
import random
def gameTracker():
global playedGames
playedGames = 1
playedGames = playedGames+1
def generateNumber():
global generatednumber
generatednumber = random.randint(1, 9)
def generateuserGuess():
global userguess
userguess = int(input('Pick a number between 1-9: '))
def generationProcess():
global userguess
if int(userguess) == generatednumber:
print('You have guessed the number. Congratulations.')
gameTracker()
global playAgain
playAgain = input('Want to play again? y/n: ')
if playAgain == 'y':
generateNumber()
userguess = input('Pick a new number between 1-9: ')
generationProcess()
if playAgain == 'n':
print('The game has ended. You have played: ')
print(int(playedGames))
print('games')
if int(userguess) > generatednumber:
print('You have guessed too high.')
global conConfirm
conConfirm = str(input('Guess again?: c/quit: '))
conGame()
if int(userguess) < generatednumber:
print('You have guessed too low.')
conConfirm = str(input('Guess again?: c/quit: '))
conGame()
def conGame():
if conConfirm == 'c':
global userguess
userguess = int(input('Your new guess please: '))
generationProcess()
if conConfirm == 'quit':
print('The game has ended. You have played: ')
print(int(playedGames))
print('games')
generateNumber()
generateuserGuess()
generationProcess()
When the user presses 'n' or 'quit' after playing it doesn't print out the right number of games which means the gameTracker()
is written badly/wrong, but it also for some reason prints the number of times the game has been played * what happens after the playAgain == 'n'.
How would I go around making this work?
working with your globals
and function returns
. I've adjusted some errors in your code and added some comments. look through!
import random
#create variable for num of played games
playedGames = 0
#creat variable to count number of guesses per game
guesses = 0
#create function to generate new random num
def generateNumber():
return random.randint(1, 9)
#askk initial question
generatednumber = generateNumber()
userguess = int(input('Pick a number between 1-9: '))
def generationProcess():
global userguess, guesses, playedGames,generatednumber
while True:
if userguess == generatednumber:
print('You have guessed the number. Congratulations.')
playedGames += 1
global playAgain
playAgain = input('Want to play again? y/n: ')
if playAgain == 'y':
generatednumber = generateNumber()
userguess = input('Pick a new number between 1-9: ')
generationProcess()
if playAgain == 'n':
print('The game has ended. You have played: '+ str(playedGames)+' games')
elif int(userguess) > generatednumber:
print('You have guessed too high.')
guesses+=1
conConfirm = str(input('Guess again?: c/quit: '))
conGame(conConfirm)
elif int(userguess) < generatednumber:
guesses+=1
print('You have guessed too low.')
conConfirm = str(input('Guess again?: c/quit: '))
conGame(conConfirm)
def conGame(conConfirm):
global userguess
if conConfirm == 'c':
userguess = int(input('Your new guess please: '))
generationProcess()
if conConfirm == 'quit':
print('The game has ended. You have played: ' + str(playedGames) + ' games')
generationProcess()