def characterName(name):
print("What would you like to name your character?")
name = input()
name = str(name)
return characterName(name)
def displayIntro():
print(name + ' are in a land full of dragons. In front of ' + name)
print(', ' + name + ' see two caves. In one cave, the dragon is friendly')
print('and will share his treasure with ' + name + '. The other dragon')
print('is greedy and hungry, and will eat ' + name + ' on sight.')
print()
为什么它一直给我类型错误?我基本上试图返回变量' name'超出了定义块,所以我可以在其他块中使用它。问题是,我试着稍微弄乱它然后我得到错误:' name'没有定义。所以我不知道自己做错了什么。谢谢!
答案 0 :(得分:0)
我看到的问题是你没有在displayIntro()中调用方法characterName()来获取某个变量中的名称。我不知道你是否想要这样,但下面是我解决问题的方法。
def characterName():
print("What would you like to name your character?")
name = input()
name = str(name)
return name
def displayIntro():
name = characterName()
print(name + ' are in a land full of dragons. In front of ' + name)
print(', ' + name + ' see two caves. In one cave, the dragon is friendly')
print('and will share his treasure with ' + name + '. The other dragon')
print('is greedy and hungry, and will eat ' + name + ' on sight.')
print()
displayIntro()