gpt4 book ai didi

python - 如何在用户输入后永久更改列表?

转载 作者:行者123 更新时间:2023-11-28 16:33:18 25 4
gpt4 key购买 nike

hiddenWords = ['hello', 'hi', 'surfing']
print("Would you like to enter a new list of words or end the game? L/E?")
decision = input()
if decision == 'L':
print('Enter a new list of words')
newString = input()
newList = newString.split()
hiddenWords.extend(newList)
j = random.randint(0, len(hiddenWords) - 1)
secretWord = hiddenWords[j]
exit(0)

如何将用户输入的内容永久添加到 hiddenWords 列表中,以便下次打开应用程序时,用户输入的词已扩展到 hiddenWords 列表中?

谢谢。本代码是代码主体的一部分。

最佳答案

当你写作时

hiddenWords = ['hello', 'hi', 'surfing']

每次程序运行时,您都将变量 hiddenWords 定义为 ['hello', 'hi', 'surfing'] 。因此,无论您在此之后扩展什么,每次代码运行上面的行时,它都会重新定义为该值。

您实际上正在寻找的是使用数据库(例如 SQLite)来存储值,以便您可以随时检索它们。另外,您可以将数据保存在一个文件中,每次都读取它,这是一种更简单的方法。

关于python - 如何在用户输入后永久更改列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29654524/

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