Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:48:52+00:00 2026-06-15T15:48:52+00:00

Possible Duplicate: referenced before assignment error in python So I’ve started to try to

  • 0

Possible Duplicate:
referenced before assignment error in python

So I’ve started to try to teach myself a little Python, and I’ve come up against my first error already.
When I try to increment ‘guesses’ (I want to be able to display how many guesses it’s taken) in the else statement I get a reference before assignment error, which I don’t understand, as I’ve assigned ‘guesses’ a value before the start of the function.

Here’s my code –

import random

def guessFunc():
    guess = input("Guess a number between 1 and 10: \n")
    guess = int(guess)

    if guess == num:
        print("Congratulations, you got it right")

    else:
        guesses += 1 
        guessFunc()


num = random.randint(1,10)
guesses = 1
guessFunc()

What confuses me more is that fact that if I put

print(guesses)

into the start of my function it will print the value I’ve assigned ‘guesses’. I really just don’t get how the function can see the value and can print it, but can’t change it.

If anyone could expalin to me why this happens, I’d be really grateful, I ‘m guessing it’s a local/global thing but I’m really not sure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:48:53+00:00Added an answer on June 15, 2026 at 3:48 pm

    Python scoping allows values from outer scopes to fall into scope for reading. So you can access and use a global from a function as you describe with the print() statement.

    However, when you want to assign, you need to use the global statement to tell Python you want to assign to a global, not a local value, meaning that if you do global guesses before you try to assign to it, it will work. As you don’t here, it will see the assignment and therefore presume you want to create a local variable, but as you are doing += it needs the existing value of the new local variable, which of course doesn’t exist, causing an error.

    Do note that globals are considered bad practice in general and are best avoided as they make it very hard to see what a function is going to do. It is far better to make the function return a value and work with that.

    A better way to do what you want would be something like this:

    import random
    
    def guessFunc():
        guesses = 0
        while guess != num:
            guess = input("Guess a number between 1 and 10: \n")
            guess = int(guess)
            guesses += 1 
        print("Congratulations, you got it right")
        return guesses
    
    num = random.randint(1,10)
    guesses = guessFunc()
    

    Firstly we use a while loop to repeat the action, as recursion is not optimized in Python, then we return the number of guesses performed from the function, and assign it to guesses rather than using globals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python nested functions variable scoping After much trial and error I have
Possible Duplicate: non static method cannot be referenced from static context hey i have
Possible Duplicate: Including FSharp.Core in a C# project: resolving type collisions I have referenced
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: C++ template, linking error I am attempting to implement a selection sort,
Possible Duplicate: Undefined Reference to I've been banging my head against this one set
Possible Duplicate: How are DLLs loaded by the CLR? In .NET are referenced DLLs
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: What is an undefined reference/unresolved external symbol error and how do I
Possible Duplicate: error when import zlib in iOS: symbol(s) not found collect2: ld I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.