1

I'm trying to create a script that plays the Name Game in text. Got stuck using my first class.

def AskName():
    print("\n\nLet's play the Name Game!\n  Based on the song written by Shirly Ellis and Lincoln Case.\n")
    GivenName = input("What is your first name? --> ")
    print("\n")
    global GivenName

Call it later on (it's the first class called) and I keep getting this... (Say I entered "David".)

./namegame.py:27: SyntaxWarning: name 'GivenName' is assigned to
before global declaration   global GivenName


Let's play the Name Game!   Based on the song written by Shirly Ellis
and Lincoln Case.

What is your first name? --> David
Traceback (most recent call last): 
File "./namegame.py", line 78, in <module>
    AskName()   File "./namegame.py", line 25, in AskName
    GivenName = input("What is your first name? --> ")
File "<string>", line 1, in <module>
NameError: name 'David' is not defined

I had GivenName as not global, and I added the following as suggested on similar issues:

if __name__== "__main__":
  AskName()

The error persists.

What am I doing wrong here?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Taizen
  • 87
  • 2
  • 11
  • 1
    You think you're on Python 3, but you're not. Get Python 3. – user2357112 Jun 14 '20 at 05:51
  • In python 2, python will attempt to compile an execute what is typed in for `input`. Python 2 uses `raw_input` for strings. Python 3 ditched the `input` functionality and renamed `raw_input` to `input`. It really looks like you are running python 2. – tdelaney Jun 14 '20 at 05:56
  • The other error is exactly what it says. You have to put `global GivenName` above any use of `GivenName` in the function. – tdelaney Jun 14 '20 at 05:57
  • You shouldn't use global variables (there are exceptions to this “rule”). Return the GivenName from the function instead. – Stefan Jun 14 '20 at 06:27
  • Thank you for the suggestions. I had tried not having GivenName as a global variable, but I tried it again. Same NameError. Using input versus raw_input didn't make a difference. Same NameError. I'll see if my version of Python is up to date. That may be it. – Taizen Jun 14 '20 at 12:28
  • I updated Python; it's now version 3.8.3. Sadly, no dice. – Taizen Jun 14 '20 at 12:51
  • Does this answer your question? [input() error - NameError: name '...' is not defined](https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined) – user202729 Jan 25 '21 at 16:14

1 Answers1

0

The mistake you are making is in global declaration of GivenName, if you are using any variable as global the line global GivenName should be always first in any function although it is not mandatory.Your code should look like this,

#if the variable is global it should be defined in global scope first and then you can use it
GivenName=""
def AskName():

    global GivenName
    print("\n\nLet's play the Name Game!\n  Based on the song written by Shirly Ellis and Lincoln Case.\n")
    GivenName = input("What is your first name? --> ")
    print("\n")

if __name__== "__main__":
  AskName()

hope this helps you!

Prathamesh
  • 1,064
  • 1
  • 6
  • 16