Python Forum
Getting Name Error: Name not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting Name Error: Name not defined
#1
Hi,

I am a new user, just at very beginning. I am trying to write a simple function but I get the name error saying that I have not defined a variable. This variable is defined in my if statement, where it should return its value. Can anyone please help me to understand what I am doing wrong. Thank you.

# A program to get three guesses from user.
# Defining guess check function.
def check_guess(letter, guess):
    if guess.isalpha():
        guess_check = letter == guess.lower()
        return guess_check
    else:
        print("Invalid input. Your guess is False.")


# creating a while loop with attempt counter
attempt = 0
print("Let\'s start the guessing game. You get three tires. \n")
while attempt < 3:
    letter_input = "j"
    attempt += 1
    print("This is your attempt #",str(attempt) + ".")
    user_guess = input("Enter an alphabet: ")
    check_guess(letter_input,user_guess)
    if guess_check == True:
        print("Your guess is correct! You won in your try #", str(attempt) + "!")
        break
    else:
        pass
Error:
"D:\Web Sync Folders\Dropbox\Study\Data Science\python_learning\venv\Scripts\python.exe" C:/Users/home/.PyCharmCE2019.1/config/scratches/scratch.py Let's start the guessing game. You get three tires. This is your attempt # 1. Enter an alphabet: 5 Traceback (most recent call last): Invalid input. Your guess is False. File "C:/Users/home/.PyCharmCE2019.1/config/scratches/scratch.py", line 19, in <module> if guess_check == True: NameError: name 'guess_check' is not defined Process finished with exit code 1
Reply
#2
Variables defined in a function are only visible within that function. You need to return guess_check from the function, and then assign it where you call check_guess. That will bring the value out of the function. See the function tutorial link in my signature below for more details.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thanks a lot Craig. It worked and helped me understand a big mistake. Thanks again.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 576 Nov-23-2023, 02:53 PM
Last Post: rob101
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,124 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,348 Mar-13-2022, 03:29 AM
Last Post: deanhystad
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,596 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,376 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,176 Dec-14-2020, 05:45 PM
Last Post: karabakh
  name error "name"is not defined MaartenRo 1 3,423 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Name Error: name 'Stockton' is not defined Pinokchu 3 2,273 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  python library not defined in user defined function johnEmScott 2 3,836 May-30-2020, 04:14 AM
Last Post: DT2000
  error ,,name append is not defined'' Killdoz 1 5,030 May-24-2020, 06:23 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020