Can anyone explain how this works? (dictionaries)
So I have written a code that took me awhile to write but in the end, after thinking about it, i honestly dont understand HOW a part of it works. The program would ask for name, age, weight, and height and put them in a file, with no limit to how many people to enter. Its written like this: age_dic = {} height_dic = {} weight_dic = {} file = open('test.txt', 'w') num = input() #enters how many people they plan to enter for x in range(num): ..... #assigns name, age, weight, and height variables age_dic[name] = age weight_dic[name] = weight height_dic[name] = height for key, value in age_dic.items(): age_des = "".join([key, ":\nAge: ", str(value)]) for key, value in height_dic.items(): height_des = "".join(["\nHeight: ", str(value)]) for key, value in weight_dic.items(): weight_des = "".join(["\nWeight: ", str (value)]) des = "".join([age_des, height_des, weight_des]) file.write(des + "\n\n") file.close() To clarify what im asking, when I enter 2 persons, they both appear on the file as intended, but what i am having trouble understanding is this: When it comes down the for loop for the second person, it adds to the dictionaries (where there are now two key:value pairs, which is proven when i print the dictionaries out in the end) but the value of the variable "des" is updated to the second person (if you print out des as the end, it would display the info of the second person, not the first). How does python know to only assign the second key value pair to the variable? Ps, sorry if i make no sense