【发布时间】:2021-01-22 09:09:24
【问题描述】:
我正在为学校制作一个基于文本的游戏,我希望它具有个性化的名称功能,但是每当我通过定义变量的函数时,其他函数只使用原始值,即 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.')
这是我拥有的确切代码段,当我点击“运行”时,它工作正常,直到 def prologue():我已经排除了它是其他东西的可能性,因为在复制器窗口中它显示“未定义”名字‘名字’”
【问题讨论】:
-
鉴于这个 sn-p 代码,
name不需要是全局的。只需定义prologue以获取名为name的参数,并在您调用prologue时将用户的输入作为参数传递。 -
这能回答你的问题吗? In Python what is a global statement?
-
感谢 chepner,但是 wdym 参数?
标签: python global-variables text-based