Home > other >  Will the while q&a into nested dictionaries, but the corresponding element in the dictionary are rep
Will the while q&a into nested dictionaries, but the corresponding element in the dictionary are rep

Time:09-26

Gathering where everyone favorite do
Hope is a dictionary answers={name_1: {location_1: hobby_1}, name_2: {location_2: hobby_2}

But now, after the input, the dictionary effect become
Answers={name_1: {location_1: hobby_1 location_2: hobby_2}, name_2: {location_1: hobby_1 location_2: hobby_2}}

My code is:
Answers={}
Names={}
While True:
Name=input (" both Please enter your name: ")
Location=input (" both Please enter your favorite place: ")
Hobby=input (" both 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)
Where there is a problem?

CodePudding user response:

While the above names dictionary should be put it inside the while loop

CodePudding user response:

1/f, advice is correct!

But if you want to more intuitive code reading effect, can be written like this:
 
Answers={}
While True:
Name=input (" both Please enter your name: ")
Location=input (" both Please enter your favorite place: ")
Hobby=input (" both 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)

CodePudding user response:

Will solve the problem in front of, boils down to, circulation data placement problems, in a written code format is beautiful
  • Related