I've nested one for loop inside of another. The first loop simply iterates over the second loop five times; the second loop iterates over the same simple block of code five times.
In total, these loops should perform the same job twenty-five times.
x = 0
for y in range(0, 5,):
for z in range(0, 5,):
print(str(int(x + 1)) + ". Hello")
I expected the output to be:
1. Hello.
2. Hello.
3. Hello.
4. Hello.
5. Hello.
Twenty-five times, with each proceeding line increasing the number's value by one.
Instead, the output was:
1. Hello
This output repeated itself twenty-five times. How do I fix this problem and receive the output I want?
xxinint(x + 1)but you don't store the result inx. Like some of the answers are suggesting, by separating the computation and storagex = x + 1orx += 1from printing the resultprint(str(x) + ". Hello"), you solve the problem. Some languages would allow you to do the whole thing in one go, but using the assignment in Python looks just like assigning a value to a named parameter, soprint(x = x + 1)confuses Python and isn't very clear to begin with.