因此,基本上我對python和編程一般來說都很陌生。我想知道,你有一個情況,你有一個字典,並詢問用戶他們是否要添加或刪除字典中的術語。因此,我知道如何在字典中添加或刪除該術語,但是如何在下次程序啓動時「保存」該數據。基本上,如果用戶在字典中添加了一個單詞,然後我問他們是否想用while循環返回主菜單,那麼如何製作它,以便他們添加的單詞現在在字典中永久保存在字典中,並返回到菜單並啓動程序結束?在python中,你如何循環字典,但保持對它做出的更改
這是我的。你要知道我是一個初學者,所以如果它看起來怪異,那麼對不起......哈哈....沒什麼大礙:
loop=None
while True:
#The initial dictionary
things={"house":"a place where you live",
"computer":"you use to do lots of stuff",
"iPod":"mp3 player",
"TV":"watch shows on it",
"bed":"where you sleep",
"wii":"a game system",
"pizza":"food"}
#Menu
print("""
Welcome to the Dictionary of Things
Choose your preference:
0-Quit
1-Look up a Term
2-Add a Term
3-Redefine a Term
4-Delete a Term
""")
choice=input("\nWhat do you want to do?: ")
elif choice=="2": #Adds a term for the user
term=input("What term do you want to add? ")
if term not in things:
definition=input("Whats the definition? ")
things[term]=definition #adds the term to the dictionary
print(term,"has been added to the dictionary")
menu=input("""
Would you like to go back to the menu?
Yes(Y) or No(N): """)
if menu=="Y":
loop=None ----->#Ok so if they want to go back to the menu the program should remember what they added
elif menu=="N":
break
顯示代碼,您演示您遇到的問題可以幫助您獲得更有用的答案。 – 2010-12-20 00:50:34
好的,我會添加它,但它可能不會很漂亮 – d8595 2010-12-20 00:57:23
優秀!我已經更新了我的答案。但在你看之前,問問自己:「當你進入循環的開始時,會發生什麼第一件事?」。 – 2010-12-20 01:18:23