Aidan Moore
Aidan Moore

Reputation: 19

Number Guessing Game - global variables

I am trying to write a program that simply generates a random number and then allows you to guess that number. The majority of the program works but I am having an issue that I believe is related to local and global variables but cannot figure out the solution. I intend to make everything simpler and more concise once it all works.

When the player makes a new guess the game should state at the beginning which number guess this is. To do this I have assigned a variable guess_num at the beginning and created the guess_count function to count-up and redefine the guess_num global variable following each guess.

When the guess_input function is run it concatenates and prints "Guess Number " followed by the guess_num variable. However instead of using the new global guess_num variable the program appears to use guess_num - 1.

At the end of the game it is sums up the number of guesses and this correctly shows the number of guesses, unlike the guess_input function.

Hopefully someone can shed some light on what I am sure is a silly mistake with a simple solution.

Number Guessing Game

import random

guess_num = 1

def guess_count():
  global guess_num
  guess_num = guess_num + 1

def guess_input():
  global guess
  print("Guess Number "+ str(guess_num))
  guess = int(input("Guess a number between " + str(low) + " and " + str(high) + ":"))
  exceed()
  guess_output()

def user_entry():
  global low
  global high
  global rand_num
  low = int(input("Type the lowest whole number the PC will generate: "))
  high = int(input("Type the highest whole number the PC will generate: "))
  print ("")
  if low > high:
    print ("ERROR: The lowest number cannont be greater than the highest")
    print ("")
    user_entry()
  rand_num = random.randint(low, high)

def exceed():
  if guess < low:
    print("")
    print("Your guess of " + str(guess) + " is less than " + str(low) + " which is the lowest possible value - try again")
    print("")
    guess_input()
  if guess > high:
    print("")
    print("Your guess of " + str(guess) + " is more than " + str(high) + " which is the highest possible value - try again")
    print("")
    guess_input()

def guess_output():
  global low
  global high
  if guess > rand_num:
    print(str(guess) + " is too high!")
    print("")
    high = guess
    guess_input()
    guess_count()
  elif guess < rand_num:
    print(str(guess) +" is too low!")
    print("")
    low = guess
    guess_input()
    guess_count()
  elif guess == rand_num:
    print ("Correct!")

def count_sum():
  print("")
  if guess_num == 1:
    print(str(guess_num) + " guess was made")
  elif guess_num > 1:
    print(str(guess_num) + " guesses were made")

user_entry()
# print(rand_num)      #used for Debugging
guess_input()
count_sum()

Upvotes: 1

Views: 315

Answers (1)

cwallenpoole
cwallenpoole

Reputation: 82058

You've swapped the order of

guess_input()
guess_count()

in your guess_output function. You need to increment the number of guesses before reporting on that number.

There are a number of architectural difficulties, but one suggestion might be move the success condition to first followed by a return, that way you won't have to make the same change in two places:

  global low
  global high

  if guess == rand_num:
    print ("Correct!")
    return

  if guess > rand_num:
    print(str(guess) + " is too high!")
    print("")
    high = guess

  elif guess < rand_num:
    print(str(guess) +" is too low!")
    print("")
    low = guess

  guess_count()
  guess_input()

Upvotes: 1

Related Questions