我想寫一個代碼,詢問不同的用戶他們的夢想度假目的地。這是一個字典,其中的關鍵是民意調查者的名字,值是一個名爲'dream_vacations'的名單。我想在創建新密鑰時創建一個新變量。什麼是最好的方式來做到這一點?如何在每次引入新密鑰時爲列表創建新變量?
vacation_poll = { }
dream_vacations = [ ]
while True:
name = input('What is your name?: ')
while True:
dream_vacation = input('Where would you like to visit?: ')
repeat = input('Is there anywhere else you like to visit? (Yes/No): ')
dream_vacations.append(dream_vacation)
if repeat.lower() == 'no':
vacation_poll[name] = dream_vacations
break
new_user_prompt = input('Is there anyone else who would like to take the poll? (Yes/No): ')
if new_user_prompt.lower() == 'no':
break
我當前的代碼不工作,因爲創建的每個按鍵都會有相同的價值觀。
它的工作原理!謝謝! – iluv3142
@ iluv3142請將問題標記爲已回答(點擊您最喜歡的答案旁邊的複選標記),這可以幫助未來的人們解決類似的問題 – Nullman