472,107 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,107 software developers and data experts.

"for x in range(1,100):" how do i make it stop once i get one right?

so im writing this program. i wont bother with posting the whole thing, cause its like 200 lines and ive barely started. (huge potensial for error, the way i like it)

anyways, in the part ive completed of my program, theres a very large chance that the program will not be able to execute. so i put this bugger on top of it:

for x in range(1,100):

when i run my program, it works like 3 or 4 times. the problem is, it then prints all the individual answers it got 3 or 4 times. so my question is, how do i make it print only the first answer it gets?

any help is apprechiated. :)
May 28 '07 #1
2 1777
ilikepython
844 Expert 512MB
so im writing this program. i wont bother with posting the whole thing, cause its like 200 lines and ive barely started. (huge potensial for error, the way i like it)

anyways, in the part ive completed of my program, theres a very large chance that the program will not be able to execute. so i put this bugger on top of it:

for x in range(1,100):

when i run my program, it works like 3 or 4 times. the problem is, it then prints all the individual answers it got 3 or 4 times. so my question is, how do i make it print only the first answer it gets?

any help is apprechiated. :)
I don't really understand what you are trying to do. Are you trying to break out of the for loop when something happens:
Expand|Select|Wrap|Line Numbers
  1. for x in range(1, 100):
  2. # something happens
  3.     break            #breaks out of the loop
  4.  
May 28 '07 #2
bartonc
6,596 Expert 4TB
so im writing this program. i wont bother with posting the whole thing, cause its like 200 lines and ive barely started. (huge potensial for error, the way i like it)

anyways, in the part ive completed of my program, theres a very large chance that the program will not be able to execute. so i put this bugger on top of it:

for x in range(1,100):

when i run my program, it works like 3 or 4 times. the problem is, it then prints all the individual answers it got 3 or 4 times. so my question is, how do i make it print only the first answer it gets?

any help is apprechiated. :)
I don't quite follow you, but it could just be a matter of indentation:
Expand|Select|Wrap|Line Numbers
  1. >>> for i in range(5):
  2. ...     print 'testing in loop %d' %i
  3. ...     print 'test value = %d' %(i**2)
  4. ...     
testing in loop 0
test value = 0
testing in loop 1
test value = 1
testing in loop 2
test value = 4
testing in loop 3
test value = 9
testing in loop 4
test value = 16
Expand|Select|Wrap|Line Numbers
  1. >>> for i in range(5):
  2. ...     print 'testing in loop %d' %i
  3. ...     
  4. # by de-indenting here only the last value shows up
  5. >>> print 'test value = %d' %(i**2)
testing in loop 0
testing in loop 1
testing in loop 2
testing in loop 3
testing in loop 4

test value = 16
>>>
May 28 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.