By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

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

P: 7
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
Share this Question
Share on Google+
2 Replies


ilikepython
Expert 100+
P: 844
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
Expert 5K+
P: 6,596
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.