1

I am making a text-based game for school, and I want it to have a personalized name feature, but whenever I get past the function where the variable is defined, the other functions only use the original value, which is 0. Here's an example:

global name = 0 #this part is at the top of the page, not actually just above the segment
def naming();
 print("A long, long time ago, there was a person who was born very ordinary, but would live to become very extraordinary.\n")
  time.sleep(2)
  while True:
    name=input("Who are you? Give me your name.\n")
    choice = input(f'You said your name was {name}, correct?\n')
    if choice in Yes:
      prologue();
    else:
      return

def prologue():
  print(f'Very well, {name}. You were born with a strange gift that nobody could comprehend, at the time. You were born with the Favor of the Gods.')

This is the exact code segment I have, and when I hit "run", It works fine until def prologue(): I have ruled out the possibility that it is something else, because in the replicator window it says "undefined name 'name'"

Rene707
  • 31
  • 5
  • 1
    Given this snippet of code, `name` doesn't need to be global. Simply define `prologue` to take a *parameter* named `name`, and pass the user's input as an argument when you call `prologue`. – chepner Oct 07 '20 at 12:03
  • Does this answer your question? [In Python what is a global statement?](https://stackoverflow.com/questions/13881395/in-python-what-is-a-global-statement) – NoDataDumpNoContribution Oct 07 '20 at 12:17
  • thanks chepner, but wdym parameter? – Rene707 Oct 07 '20 at 13:27

3 Answers3

0

global is used inside a function to indicate that a name that would otherwise be treated as a local variable should be global instead.

def naming():
    global name

    ...

def prologue():
    print(f'Very well, {name}. ...')

As long as you don't call prologue before you call name, there is no need to initialize name in the global scope; the assignment inside naming is sufficient.


Also, you meant choice in ["Yes"] or, better yet,choice == "Yes"

chepner
  • 497,756
  • 71
  • 530
  • 681
  • for naming variable, how would I make a variable update so that it is the user's input for the rest of the code? And no, 'Yes' is the name of an input variable – Rene707 Oct 07 '20 at 12:11
  • What do you mean,"update"? Once you set the global `name`, it is available to the rest of your code (at least, the rest of the code sharing the same global namespace). – chepner Oct 07 '20 at 12:47
  • I mean that when I define it with user input, it reverts to 0 (default value) when I start a new function – Rene707 Oct 07 '20 at 13:21
  • It doesn't *need* a default value; get rid of that assignment. (It's not clear how that assignment could be executed again anyway.) – chepner Oct 07 '20 at 13:22
  • But when I assign it as a global and don't define it, it just has an error, and says "name" is not defined – Rene707 Oct 07 '20 at 14:46
0

Remove global from name then it should work

0

This is a working example but isnt it better that you pass name to prologue function instead of using global variable? It is another subject but you have to avoid using global.

import time

name = 0 #this part is at the top of the page, not actually just above the segment
def naming():
    global name
    print("A long, long time ago, there was a person who was born very ordinary, but would live to become very extraordinary.\n")
    time.sleep(2)
    while True:
        name=input("Who are you? Give me your name.\n")
        choice = input(f'You said your name was {name}, correct?\n')
        if choice == "Yes":
          prologue()
        else:
          return

def prologue():
    global name
    print(f'Very well, {name}. You were born with a strange gift that nobody could comprehend, at the time. You were born with the Favor of the Gods.')


if __name__ == '__main__':
    naming()
obayhan
  • 1,636
  • 18
  • 35