简体   繁体   English

为什么我的全局变量不起作用? (Python)

[英]Why is my global variable not working? (Python)

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:我正在为学校制作一个基于文本的游戏,我希望它具有个性化的名称功能,但是每当我越过定义变量的 function 时,其他函数只使用原始值,即 0。这是一个例子:

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'"这是我拥有的确切代码段,当我点击“运行”时,它工作正常,直到 def prologue(): 我已经排除了它是其他东西的可能性,因为在复制器 window 中它说“未定义的名称'名称'"

global is used inside a function to indicate that a name that would otherwise be treated as a local variable should be global instead. globalfunction 中使用,表示原本会被视为局部变量的名称应该是 global。

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;只要在调用name之前不调用prologue ,就不需要在全局 scope 中初始化name the assignment inside naming is sufficient. naming中的赋值就足够了。


Also, you meant choice in ["Yes"] or, better yet, choice == "Yes"另外,您的意思choice in ["Yes"]或者更好的是, choice == "Yes"

Remove global from name then it should work从名称中删除全局然后它应该工作

This is a working example but isnt it better that you pass name to prologue function instead of using global variable?这是一个工作示例,但将名称传递给序言function 而不是使用全局变量不是更好吗? 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM