Reputation: 23
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?
Upvotes: 2
Views: 55
Reputation: 1
you can also use this :
i = 0
for y in range(0, 5):
for z in range(0, 5):
i = i+1
print(str(i) + "." " Hello.")
Upvotes: 0
Reputation: 1327
You aren't updating the value for x
as you loop through.
Try this:
x = 0
for y in range(0, 5,):
for z in range(0, 5,):
x+=1
print(str(x) + ". Hello")
Upvotes: 1
Reputation: 2882
You are almost there. Just add one extra line
x = 0
for y in range(0, 5,):
for z in range(0, 5,):
print(str(int(x + 1)) + ". Hello")
x += 1
Upvotes: 0