我正在做一个迷你文字角色扮演游戏,你必须与国王共进晚餐。我基本上是在声明if语句中的变量,根据用户输入的内容,使变量成为一个特定的值。我想我需要在函数之外声明它们,但我不确定我对python相当陌生。
def creation_role():
print("""What is your character's role?\n
A: Lord (Base stats: Charisma 7, Wit 3, Valor 5)
B: Peaseant (Base stats: Charisma 5, Wit 7, Valor 3)
C: Knight (Base stats: Charisma 3, Wit 5, Valor 7)""")
role = input("> ")
role = role.lower()
if "a" in role:
role = "lord"
charisma = 7
wit = 3
valor = 5
elif "b" in role:
role = "peaseant"
charisma = 5
wit = 7
valor = 3
elif "c" in role:
role = "knight"
charisma = 3
wit = 5
valor = 3
else:
error()
creation_role()
def creation_home():
print("""Choose where you hail from:\n
A. Hillford: Lush foliage as far as the eyes can see, the enchanted trees
bear the fruit of wisdom.\n
B. Aermagh: A land as beautiful as it is cold, it's people learn strength
from the first day of life fighting frostbite as a babe.\n
C. Venzor: The rich island nation some call it the pearl of Skystead,
In recent decades it's seen outsiders giddy to make quick riches.""")
home = input("> ")
home = home.lower()
if "a" in home:
home = "Hillford"
wit + 2
elif "b" in home:
home = "Aermagh"
valor + 2
elif "c" in home:
home = "Venzor"
charisma + 2
else:
error()
creation_home()
def test():
creation_role()
creation_home()
print(home, "\n", charisma, "\n", wit, "\n", valor)
test()发布于 2019-03-25 02:39:13
测试函数不知道主变量、魅力变量、智慧变量和勇敢变量。
为了让它了解它们,您需要从其他函数中返回这些值。例如:
def creation_role():
...logic
return role, charisma, wit, valor然后,您可以在测试中使用这些变量。
def test():
role, charisma, wit, valor = creation_role()
home, charisma, wit, valor = creation_home(charisma, wit, valor)
print(home, "\n", charisma, "\n", wit, "\n", valor)注意,还需要修改creation_home函数以接受这些变量,并返回新值。
def creation_home(charisma, wit, valor):
....
return home, charisma, wit, valor编辑:在修改统计数据时还会出现一些语法错误。它应该是
wit += 2而不是
wit + 2发布于 2019-03-25 02:37:12
您应该创建类MiniText或其他什么,然后将这些变量初始化为init的一部分。我看到的另一个问题是
charisma + 2您没有存储结果值。
https://stackoverflow.com/questions/55330491
复制相似问题