gpt4 book ai didi

Python:获取函数中定义的变量的值,在该函数之外

转载 作者:太空宇宙 更新时间:2023-11-04 08:49:53 25 4
gpt4 key购买 nike

我需要一种方法来获取在该函数之外的函数中定义的变量的值。这不是必需的,但会使我的代码更容易理解。目前还没有找到任何可靠的答案。

def pizzais():
pizza = "yummy"

pizzais()
print(pizza)

这将返回一个错误,指出未定义披萨。有没有什么技巧可以解决这个问题。

太了解我的情况了,这里是我的代码,我也在应用它。

def questions():
user = input("What is your username?") #username
race = input("What is your race? (orc, human, elf, goblin)") #race

#won't move on if the player fills an answer that is not an option
if race == "orc" or race == "human" or race == "elf" or race == "goblin":
pClass = input("What is your class? (archer, warrior, rogue or mage)")


else:
while race != "orc" and race != "human" and race != "goblin" and race != "elf":
race = input("What is your race? (orc, human, elf, goblin)")
if race == "orc" or race == "human" or race == "elf" or race == "goblin":
pClass = input("What is your class? (archer, warrior, rogue or mage)")

#won't move on if the player fills an answer that is not an option
if pClass == "archer" or pClass == "warrior" or pClass == "rogue" or pClass == "mage":
correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")

else:
while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
if pClass == "archer" or pClass == "warrior" or pClass == "rogue" or pClass == "mage":
correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")

def correct_def():
correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")
if correct == "yes": #if player likes their choices the game will begin
print("Enjoy the game " + user + "!")

elif correct == "no": #if player doesn't like their choices all questions are asked again
reAsk = input("What would you like to change?(username, race, class or all)")

else:
while correct != "yes" and correct != "no":
correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")
if correct == "yes":
print("Enjoy the game " + user + "!")

elif correct == "no":
questions()

if correct == "yes": #if player likes their choices the game will begin
print("Enjoy the game " + user + "!")

elif correct == "no": #if player doesn't like their choices all questions are asked again
reAsk = input("What would you like to change?(username, race, class or all)")

if reAsk == "username":
user = input("What is your username?")
correct_def()

elif reAsk == "race":
race = input("What is your race? (orc, human, elf, goblin)")
while race != "orc" and race != "human" and race != "goblin" and race != "elf":
race = input("What is your race? (orc, human, elf, goblin)")
correct_def()

elif reAsk == "class":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
correct_def()

elif reAsk == "all":
questions()

else:
while reAsk != "username" and reAsk != "race" and reAsk != "class" and reAsk != "all":
reAsk = input("What would you like to change?(username, race, class or all)")
if reAsk == "username":
user = input("What is your username?")
print("Enjoy the game " + user + "!")

elif reAsk == "race":
race = input("What is your race? (orc, human, elf, goblin)")
while race != "orc" and race != "human" and race != "goblin" and race != "elf":
race = input("What is your race? (orc, human, elf, goblin)")
print("Enjoy the game " + user + "!")

elif reAsk == "class":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
print("Enjoy the game " + user + "!")

elif reAsk == "all":
questions()

#won't move on if the player fills an answer that is not an option
else:
while correct != "yes" and correct != "no":
correct = input("So you are " + user + ", the " + race + " " + pClass + "? (yes or no)")
if correct == "yes":
print("Enjoy the game " + user + "!")

elif correct == "no":
reAsk = input("What would you like to change?(username, race, class or all)")

if reAsk == "username":
user = input("What is your username?")
correct_def()

elif reAsk == "race":
race = input("What is your race? (orc, human, elf, goblin)")
while race != "orc" and race != "human" and race != "goblin" and race != "elf":
race = input("What is your race? (orc, human, elf, goblin)")
correct_def()

elif reAsk == "class":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
correct_def()

elif reAsk == "all":
questions()

else:
while reAsk != "username" and reAsk != "race" and reAsk != "class" and reAsk != "all":
reAsk = input("What would you like to change?(username, race, class or all)")
if reAsk == "username":
user = input("What is your username?")
print("Enjoy the game " + user + "!")

elif reAsk == "race":
race = input("What is your race? (orc, human, elf, goblin)")
while race != "orc" and race != "human" and race != "goblin" and race != "elf":
race = input("What is your race? (orc, human, elf, goblin)")
print("Enjoy the game " + user + "!")

elif reAsk == "class":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
while pClass != "archer" and pClass != "warrior" and pClass != "rogue" and pClass != "mage":
pClass = input("What is your class? (archer, warrior, rogue or mage)")
print("Enjoy the game " + user + "!")

elif reAsk == "all":
questions()
questions()

最佳答案

您可能应该通读 Tutorial section on functions - 这只是您使用函数for 的最基本的东西。

您的代码示例一团糟,但这里有一个您似乎要尝试做的简单示例:

def ask_race():
race = None
options = ['orc', 'human', 'elf', 'goblin']
while race not in options:
race = input('What is your race? ({})'.format(','.join(options)))
return race

重复您的其他选项,然后您可以执行以下操作:

def questions():
race = ask_race()
character_class = ask_class()

还有更多的清理工作可以完成,但这应该让您开始使用更合理的结构。

关于Python:获取函数中定义的变量的值,在该函数之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36489437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com