简体   繁体   English

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

[英]How do I permanently change a list after user's input?

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)

How do I permanently add the input of the user to the hiddenWords list so that next time I open the application the words the user has entered has been extended onto the hiddenWords list? 如何将用户的输入永久添加到hiddenWords列表中,以便下次打开应用程序时,用户输入的单词已扩展到hiddenWords列表?

Thanks. 谢谢。 This Code is part of a main body of code. 本准则是代码主体的一部分。

When you write 当你写作

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

You are, each time the program runs, defining the variable hiddenWords as ['hello', 'hi', 'surfing'] . 每次程序运行时,您都将变量hiddenWords定义为['hello', 'hi', 'surfing'] So no matter what you extend after this, every time the code runs the line above, it will redefine to that value. 因此,无论您在此之后延伸,每次代码运行上面的行时,它都将重新定义为该值。

What you are looking for actually is to use a Database, such as SQLite, to store values so that you can retrieve them at any time. 您实际需要的是使用数据库(如SQLite)来存储值,以便您可以随时检索它们。 Also, you can save data in a file and read this everytime, which is a simpler way. 此外,您可以将数据保存在文件中并每次都读取,这是一种更简单的方法。

When your program exits, all variables are lost, because variables only exit in memory. 当程序退出时,所有变量都会丢失,因为变量只会在内存中退出。 In order to save your modification accross program executions (everytime you run your script), you need to save the data onto the Disk, ie: write it to a file. 为了在程序执行期间保存修改(每次运行脚本时),都需要将数据保存到磁盘上,即:将其写入文件。 Pickle is indeed the simplest solution. Pickle确实是最简单的解决方案。

I like json. 我喜欢json。 This would be a possible solution: 这将是一个可能的解决方案:

import json

words = []
try:
  f = open("words.txt", "r")
  words = json.loads(f.read())
  f.close()
except:
  pass

print("list:")
for word in words:
  print(word)
print("enter a word to add it to the list or return to exit")
add = raw_input() # for python3 you need to use input()

if add:
  words.append(add)
  try:
    f = open("words.txt", "w")
    f.write(json.dumps(words, indent=2))
    f.close()
    print("added " + add)
  except:
    print("failed to write file")

If you want to add multiple words at a time use this. 如果要一次添加多个单词,请使用此选项。

import json

words = []
try:
  f = open("words.txt", "r")
  words = json.loads(f.read())
  f.close()
except:
  pass

print("list:")
for word in words:
  print(word)

save = False
while True:
  print("enter a word to add it to the list or return to exit")
  add = raw_input() # for python3 you need to use input()
  if add:
    words.append(add)
    print("added " + add)
    save = True
  else:
    break

if save:
  try:
    f = open("words.txt", "w")
    f.write(json.dumps(words, indent=2))
    f.close()
  except:
    print("failed to write file")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM