0

import random

#Introduction
print("Number guessing game with 2 difficulties, easy and hard.\nEasy: Numbers 0 to 10 with 3 guesses.\nHard: Numbers 0 to 20 with 5 guesses.\nWinning an easy game gives you 1 point while winning a hard game gives you 2 points.\n")

#Variables
Easy_Points = 0
Hard_Points = 0
Total_Points = Easy_Points + Hard_Points

#Function that manages the display of options and the input
def Input():
  global Option
  print("0: Exit\n1: See points\n2: Easy\n3: Hard\nOnly numbers 0 to 2 are accepted.\n")
  Option = input("Difficulty: ")
  if Option == "0":
    print("Exiting...")
    exit()
  if Option == "1":
    Score("Points", "Yes")
  if Option == "2":
    Difficulty = "Easy"
    print("Difficulty: " + Difficulty + "\n")
    Easy_Game()
  elif Option == "3":
    Difficulty = "Hard"
    print("Difficulty: " + Difficulty + "\n")
    Hard_Game()
  else:
    print("Only numbers 1 to 10 are accepted.\n")
    Input()

#Function that manages the game for the easy difficulty
def Easy_Game():
  Guess_Count = 0
  Guess_Limit = 3
  Lucky_Number = random.randrange(11)
  while Guess_Count < Guess_Limit:
    try:
      Guess = int(input("Enter your guess: "))
      Guess_Count += 1
      if Guess < Lucky_Number:
        print("Number is bigger!\n")
      elif Guess > Lucky_Number:
        print("Number is smaller!\n")
      elif Guess == Lucky_Number:
        print("You won!\n")
        Score("Easy", "No")
        Input()
        break
    except ValueError:
      print("Only numbers 1 to 10 are accepted.\n")
      Easy_Game()
  if Guess_Count == Guess_Limit:
    print("You lost!")
    Input()

#Function that manages the game for hard difficulty
def Hard_Game():
  Guess_Count = 0
  Guess_Limit = 5
  Lucky_Number = random.randrange(21)
  while Guess_Count < Guess_Limit:
    try:
      Guess = int(input("Enter your guess: "))
      Guess_Count += 1
      if Guess < Lucky_Number:
        print("Number is bigger!\n")
      elif Guess > Lucky_Number:
        print("Number is smaller!\n")
      elif Guess == Lucky_Number:
        print("You won!\n")
        Score("Hard", "No")
        Input()
        break
    except ValueError:
      print("Only numbers 1 to 20 are accepted.\n")
      Hard_Game()
  if Guess_Count == Guess_Limit:
    print("You lost!")
    Input()

def Score(Difficulty, See_Points):
  if Difficulty == "Easy" and See_Points == "No":
    Easy_Points += 1
    print("Only the words \"Yes\" or \"No\" are accepted.")
    See_Points = input("See total points? ")
    if See_Points == "Yes":
      print("Total points: " + Total_Points)
      Input()
    elif See_Points == "No":
      Input()
    else:
      print("Invalid input.")
  elif Difficulty == "Hard" and See_Points == "No":
    Hard_Points += 2
    print("Only the words \"Yes\" or \"No\" are accepted.")
    See_Points = input("See total points? ")
    if See_Points == "Yes":
      print("Total points: " + Total_Points)
      Input()
    elif See_Points == "No":
      Input()
    else:
      print("Invalid input.")
  elif Difficulty == "Points" and See_Points == "Yes":
    print("Total points: " + str(Total_Points))

The error occurs in the last function, Score(Difficulty, See_Points). When I enter Score(“Easy”, “No”), I end up with the UnboundLocalError error, where Easy_Points and Hard_Points are referenced before the assignment. I have tried many ways to solve the problem but am unable to do so. However, if I enter Score(“Points”, “Yes”) the function can work. How do I fix this problem?

ash
  • 988
  • 1
  • 3
  • 17
  • It answered my question. However, even after fixing it, the script still does not work as intended. Trying to fix it now. – Cedrick Tan Mar 15 '22 at 14:42

0 Answers0