Alekong
Alekong

Reputation: 13

python guessing game excersise

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?

Upvotes: 1

Views: 1299

Answers (4)

Daniil Kiselev
Daniil Kiselev

Reputation: 3

I think there is a simpler way

from random import randrange
a = int(randrange(10))
running = True
options = [1, 2, 3, 4, 5, 6, 7, 8, 9]


while running:
    result = None

    while result not in options:
        result = int(input("quess the number: "))

    if a == result:
        print("Yeah you are right!")
        options.append("win")

    elif a < result:
        print("Nope, too high")
        options.append("lose")

    elif a > result:
        print("Nope, too low")
        options.append("lose")

    if not input("Play again? (y/n): ").lower() == "y":
        running = False

Upvotes: 0

Chigo Godwin Anyaso
Chigo Godwin Anyaso

Reputation: 209

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()

Upvotes: 1

Specas
Specas

Reputation: 98

In your gameTracker() code, you initializeplayedGames to 1 and then increment it. So it looks like it would always store the value 2.

You get multiple outputs because of your recursion. After the last recursive call, the program control goes back to the if statement and the value of the variable still being 'n' or 'quit' would print the details again. You can initialize the variable to a different value after the exit conditions to avoid this.

import random

playedGames = 0

def gameTracker():
    global playedGames
    playedGames = playedGames+1

def generateNumber():
    global generatednumber
    generatednumber = random.randint(1, 9)
    gameTracker()

def generateuserGuess():
    global userguess
    userguess = int(input('Pick a number between 1-9: '))


def generationProcess():
    global userguess
    global generatednumber
    if int(userguess) == generatednumber:
        print('You have guessed the number. Congratulations.')
        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')
            playAgain = 'x'


    if int(userguess) > generatednumber:
        print('You have guessed too high.')
        global conConfirm
        conConfirm = 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():
    global conConfirm
    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')
          conConfirm = 'x'

generateNumber()
generateuserGuess()
generationProcess() 

Upvotes: 0

briblue3
briblue3

Reputation: 33

You should increase the number of games played within the generateNumber() method after declaring it in the gameTracker() method. That way you can keep track of how many games have been played no matter how many are played! It would look something like this:

def gameTracker():
    global playedGames
    playedGames = 0

def generateNumber():
    global generatednumber
    generatednumber = random.randint(1, 9)
    playedGames += 1

Upvotes: 1

Related Questions