我不想手動輸入所有想在我的文本遊戲中使用的怪物,而是想通過IDLE自動添加到字典中。我的問題是如何收集和保存我在IDLE中添加的新怪物。有沒有一種方法可以製作一個將信息寫入記事本的程序?我尋找類似的答案,並沒有看到任何東西。但我不一定知道正確的術語。謝謝!字典創建者(Python 3.x)
"""
Dictionary of indexed "monsters" for text game
"""
### Premade dictionary
mondic = {1:'dragon',
2:'angry gnome vigilante',
3:'Donald Trump',
4:'lambhorn rider',
5:'bone cancer'}
print(mondic)
check = 'yes'
x = 5
### Create new monsters
while check == 'yes':
print('Do you want to create a monster?')
check = str(input('yes/no '))
if check == 'no':
break
new_monster = str(input('New monster name: '))
x += 1
mondic[new_monster] = x
print(mondic)
寫入記事本?你的意思就像你在記事本中打開的文本文件?如果是這樣,當然。它只是寫入一個文件。 – skrrgwasme
考慮'pickle'或'json'模塊? – ShadowRanger
你似乎有'mondic [new_monster] = x',你的意思是'mondic [x] = new_monster'。 –