【发布时间】:2020-09-30 16:29:16
【问题描述】:
我这里有这段代码:
def alist():
wor = []
definition = []
wele = str(input("Enter a word: "))
dele = str(input("Enter a definition: "))
x = wor.append(wele)
y = def.append(dele)
print("Words' List: ", wor, "\nDefinitions' List: ", definition)
每当我运行它时,我都可以将元素添加到列表wor 和def,但是,每当我再次运行它时,它会覆盖我之前第一次运行它时添加的元素。我通过将wor 和def 转换为全局变量来避免这个问题。除了将这两个列表做成全局变量,还有其他方法吗?
谢谢!
【问题讨论】:
-
def 是 python 关键字,不要将其用作变量名
-
有你的功能
return你需要什么。
标签: python list input global-variables