收集每個人在什么地方最喜歡做什么
希望達成的字典是 answers = {name_1:{location_1:hobby_1},name_2:{location_2:hobby_2}
但是現在輸入后,出來的字典效果變成
answers = {name_1:{location_1:hobby_1,location_2:hobby_2},name_2:{location_1:hobby_1,location_2:hobby_2}}
我的代碼是:
answers = {}
names = {}
while True:
name = input("Please enter your name:")
location = input("Please enter your favorite place:")
hobby = input("Please enter the hobby what you are going to do:")
answers[name] = names
names[location] = hobby
repeat = input("If you want another people to respond?(yes/no)")
if repeat == 'no':
break
print(answers)
是哪里有問題呢?
uj5u.com熱心網友回復:
while上面的names字典應該要放在while回圈里面吧
uj5u.com熱心網友回復:
1樓的建議是正確的!不過如果要更直觀的代碼閱讀效果,可以寫成這樣:
answers = {}
while True:
name = input("Please enter your name:")
location = input("Please enter your favorite place:")
hobby = input("Please enter the hobby what you are going to do:")
value=https://bbs.csdn.net/topics/{location:hobby}
answers.update({name:value})
repeat = input("If you want another people to respond?(yes/no)")
if repeat == 'no':
break
print(answers)
uj5u.com熱心網友回復:
前面的都將這個問題解決了,歸結為,回圈資料放置位置的問題,在一個,書寫代碼格式美觀的問題轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/80774.html
上一篇:python安裝出現問題
