5

I'm a beginner in python and using v2.7.2 here's what i tried to execute in the command prompt

 p = 2
 while(p>0):
     for i in range(10):
          print i+1 , p
          p-=1

The expected output was

1 2
2 1

However the actual output is

1 2
2 1
3 0
4 -1
5 -2
6 -3
7 -4
8 -5
9 -6
10 -7

Why does this happen? and How do i achieve the expected behavior?

5
  • Is that really the output or does it go all the way up to 10? Commented Jun 28, 2013 at 11:42
  • @doctorlove it definitely is not the output OP gets. Commented Jun 28, 2013 at 11:44
  • Sorry! I made an edit to it! Thanks! Commented Jun 28, 2013 at 11:44
  • Your code doesn't produce the actual output you print. They way you write it, only the first five lines are shown. Commented Jun 28, 2013 at 12:12
  • looks like someone edited it again! i'll fix it thanks for noticing. Commented Jun 28, 2013 at 12:14

3 Answers 3

6

The while condition is only tested again after the for loop finishes. You could do this instead

p = 2
for i in range(10):
    if p <= 0:
        break
    print i+1 , p
    p-=1
Sign up to request clarification or add additional context in comments.

Comments

5

This is the output I get:

1 2
2 1
3 0
4 -1
5 -2
6 -3
7 -4
8 -5
9 -6
10 -7

Your question as to why it runs. Your outer most conditional is a while loop, which is true upon the first execution, however it runs right into a nested for loop. When this happens the while, will not be checked until the for loop finishes its first execution (which is why p = -7).

What you want is this:

p = 2
for i in range(10):
    if p <= 0:
        break
    print i+1 , p
    p-=1

which gives output:

1 2
2 1

8 Comments

can you please make it 1 2 2 1 Thats what i'm looking for thanks!
@JosyulaKrishna fixed. Thanks for catching that.
No, now i isn't changing
@gnibbler I just ran it in ideone and included the link to the output.
Yes, now it matches my answer exactly :p
|
0

Welcome to python :-)

You can try this:

p = 2
for i in range(1, p+1):
    print i,
print
for i in range(p, 1, -1):
    print i,

This will print the desired output. Not only for 2 but also if you change the value of p, it will print the same output.

range function can take three arguments viz start, stop and step. If you give range(x), it will take start as 0, and step as 1 and go up to x(x is not included). If you give range(y, x), it will start from y and go up to x with default step value as 1.

In the example, I used range(p, 0, -1) which takes start point as 'p', step as -1 and decrement the value of p until 1 and quits. To read more about range, click here.

2 Comments

Thank you! however i'm trying to solve it by using conditionals.
@JosyulaKrishna Conditionals? You mean through if statement or through while loop?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.