-1
x=0
y=0

while 1==1:

    while y!=5:
        y=y+1
        print(str(x) + str(y))


    else:
        x=x+1
        #NOW GO TO WHILE 1==1 AND DO THAT AGAIN 

This code should print 01; 02; 03; 04; 05 and then it should print 11; 12; 13; 14; 15. But in reality it does only the first five prints because I don't know how to get to the start again after else:.

EDIT: I am so sorry, i tried to make the code easier to understand and i made few mistakes instead, that werent really a problem.

Bob
  • 347
  • 2
  • 3
  • 12
  • 4
    what do you mean? It always keeps looping. It always goes back to the `while 1 == 1`. The only thing is, `y` is incremented until it is equal to `5`, so no printing is done inside the loop anymore. – Willem Van Onsem Feb 14 '17 at 21:25
  • ...`continue`? And don't forget to reset `y = 0`. – Aran-Fey Feb 14 '17 at 21:27
  • The second while loop won't be entered after y = 5. You're problem doesn't lie in the going from `else` to the beginning, its that your logic doesn't make sense. Also you are using `else` without an `if` before hand, which will not work. – Aroic Feb 14 '17 at 21:28
  • `else` after `while` is [valid](http://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement) but kinda weird. – Eric Duminil Feb 14 '17 at 21:43

5 Answers5

2

Here's a working code with a similar structure than yours :

x = 0
y = 0

while x != 2:
    while y != 5:
        y = y + 1
        print(str(x) + str(y))
    else:
        y = 0
        x = x + 1

But please don't do that. Instead :

for x in range(2):
    for y in range(5):
        print '%d%d' % (x,y+1)
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • 1
    And if you're using Python 2, `xrange` would be a better idea than `range`. – Fred Larson Feb 14 '17 at 21:43
  • @FredLarson: I cannot please everybody. Yesterday someone complained because I used `xrange` ;) Also, given the code in OP, I think correct results and readability come before performance IMHO. – Eric Duminil Feb 14 '17 at 21:44
  • 2
    I didn't intend to criticize your answer at all. Just adding a bit. It probably doesn't really make any difference in code this trivial. – Fred Larson Feb 14 '17 at 21:50
  • Thanks for the clarification and the suggestion. I just felt it was a kind of "Damned if you do, damned if you don't" situation. – Eric Duminil Feb 14 '17 at 21:52
  • Thank you so much! I just didnt realized that i have to set the value back to 0. I should probably take a rest before making stupid annoying posts.... – Bob Feb 15 '17 at 08:13
0

I would say a better approach is to do a nested for loop.

0
from itertools import count

for x in count():
    [print('{}{}'.format(x, y)) for y in range(1, 6)]

and it's Pythonic (hope that wasn't your homework).

SARose
  • 3,558
  • 5
  • 39
  • 49
0

Just remove else: and use formatted print to avoid printing the sum.

A better version of your code is:

x = 0
while 1 == 1:
    y = 1
    while y <= 5:
        print '%d%d' % (x,y)
        y = y+1
    x = x+1
  • Thanks for the catch, bu that wasnt really a problem i was just stupid and make few mistakes while trying to rewrite original code to make it easier. – Bob Feb 15 '17 at 08:16
0

First of all your code outputs:

1
2
3
4
5

and then stops. What you are asking for is this:

01
02
03
04
05
11
12
13
[...]

To get this output you need an infinite loop which continuously increments x to do this start off with this piece of code:

x = -1
while True:
    x += 1

You then need a loop which will increment y from 1 to 5 and print the string concatenation of x and y:

for y in range(5):
    print(str(x) + str(y+1))

Nest the for loop in the while loop and voila!

x = -1
while True:
    x += 1
    for y in range(5):
        print(str(x) + str(y+1))
Joseph Chotard
  • 666
  • 5
  • 15