(python) why the result of this code not in right order?
Question:
input:
D = {"apple":44, "cherry":"game"}
for x in D:
print(x, D[x])
print(str(x) + ": "+ str(D[x]))
output:
apple 44
apple: 44
cherry game
cherry: game
Isn’t this supposed to be outputted like this in right order?
apple 44
cherry game
apple: 44
cherry: game
Answers:
In each loop you:
- print the label and its value;
- print the label converted to sting and its value converted to string.
So in the first cycle you have prints on element "apple":44, and in the second one prints on "cherry":"game".
The output is totally right.
X start out as apple the first iteration and then goes to cherry the secound iteration
input:
D = {"apple":44, "cherry":"game"}
for x in D:
print(x, D[x])
print(str(x) + ": "+ str(D[x]))
output:
apple 44
apple: 44
cherry game
cherry: game
Isn’t this supposed to be outputted like this in right order?
apple 44
cherry game
apple: 44
cherry: game
In each loop you:
- print the label and its value;
- print the label converted to sting and its value converted to string.
So in the first cycle you have prints on element "apple":44, and in the second one prints on "cherry":"game".
The output is totally right.
X start out as apple the first iteration and then goes to cherry the secound iteration