943,148 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 426
  • Python RSS
Feb 8th, 2010
0

Help with loops and when they end? for a newbie?

Expand Post »
Hi, im a begginer to python, and i've been doing it a small while now, i've been attempting some puzzles of a website, and have met a small problem, and i can't find it. My code is below and i will explain my problem, ask how to sort it, and ask if i am missing something important.
Python Syntax (Toggle Plain Text)
  1. a = range(1,1000)
  2. b = []
  3. d = 0
  4. for n in a:
  5. while n != 1:
  6. if n % 2 == 0:
  7. b.append(n)
  8. n = n/2
  9. if n % 2 != 0:
  10. b.append(n)
  11. n = n*3 + 1
  12. else: b.append(n)
  13. if len(b) > d:
  14. d = len(b)
  15. b = []
Now the aspects of this code is to find out how long it takes to get to 1, using the 'Collatz Problem' i take the value of n to be inside the range, and if its not 1, i want it to go through the number, decide if its even or odd, then perform a calculation, firstl, im recording the number in a list, so i know the starting number, and then it will perform the calculation, i want it to continue this loop until it reaches 1, im not sure how to do this, i have the else statement at ehd end because i believe that is what i need, i then will take the length of b which is my list, and see if it is higher than my previous list, i then want it to record if so and erase the list, so i can start over at 0, with my next number, but i seem to not be able to make my list work, and i get memory failure of B several times, if someone can help, it would really be apprechiated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sammyboy289 is offline Offline
5 posts
since Feb 2010
Feb 8th, 2010
0
Re: Help with loops and when they end? for a newbie?
So in summary (for lazy to read like me), what is the issue? Doesn't work as intended or throws exception? What is the error?
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Feb 8th, 2010
0
Re: Help with loops and when they end? for a newbie?
The error is, my b statement doesn't keep resetting which it should, and my list continues even once its reached 1, which i dont understand why also, im not sure if my else: statement is in the right place. Sorry i write a lot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sammyboy289 is offline Offline
5 posts
since Feb 2010
Feb 8th, 2010
0
Re: Help with loops and when they end? for a newbie?
Let's see what happens when n is 2. Your first if condition is true, you divide n by 2 and it becomes 1. Now your second if condition is also true! Welcome to the infinite loop.
Reputation Points: 707
Solved Threads: 185
Practically a Posting Shark
nezachem is offline Offline
843 posts
since Dec 2009
Feb 8th, 2010
0
Re: Help with loops and when they end? for a newbie?
should it loop, i didn't think it would, because thats while n != 1, so it should stop that loop and move onto the else: statement, or is that incorrect? i can't quite work out, how to make 2 loops, and when one ends, go to the next statement. That's the main problem im dealing with, sorry if im explaining wrong or being really useless
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sammyboy289 is offline Offline
5 posts
since Feb 2010
Feb 8th, 2010
0
Re: Help with loops and when they end? for a newbie?
Shorten the list and add a temporary print for testing. See what that will do for you:
Python Syntax (Toggle Plain Text)
  1. # shorten list for test
  2. a = range(1, 10)
  3. b = []
  4. d = 0
  5. for n in a:
  6. while n != 1:
  7. if n % 2 == 0:
  8. b.append(n)
  9. n = n/2
  10. elif n % 2 != 0:
  11. b.append(n)
  12. n = n*3 + 1
  13. else: b.append(n)
  14. print b, d, len(b) # test
  15. if len(b) > d:
  16. d = len(b)
  17. b = []
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Feb 8th, 2010
0
Re: Help with loops and when they end? for a newbie?
At run time, things happen one step at a time. Let's trace the execution of your program with n being 2

while n != 1:
    # Python checks that n is not 1, and enters a loop
    if n % 2 == 0:
        # Python checks that n is even and enters the if-clause
        ...
        n = n/2 # n becomes 1
    if n % 2 != 0:
        # Remember, n is 1, an odd number, so python enters the if-clause
        ...
        n = n*3 + 1 # n becomes 4
    # while loop goes into next iteration, n is 4
while n != 1:
    # n is 4, enter the loop
    if n % 2 == 0:
        # n is even, enter the if-clause
        ...
        n = n/2 # n becomes 2
    if n % 2 != 0: # n is even, if-clause is not entered.
# Next iteration of while loop. n is 2. Etc, etc, etc
Reputation Points: 707
Solved Threads: 185
Practically a Posting Shark
nezachem is offline Offline
843 posts
since Dec 2009
Feb 8th, 2010
0
Re: Help with loops and when they end? for a newbie?
wow, that worked, thank you so much, if its not too much to ask, do you mind saying how you changed it, and what it does?, because i notice the elif: statement which im not good with, and one question about the last part, as i will only want one resut to show, will i need to change
Python Syntax (Toggle Plain Text)
  1. #
  2. print b, d, len(b)
  3. if len(b) > d:
  4. d = len(b)
  5. b = []
to something like
Python Syntax (Toggle Plain Text)
  1. print b, d, len(b)
  2. if len(b) > d:
  3. d = len(b)
  4. b = []
  5. if len(b) < d:
  6. b = []
and thank you very much
Last edited by sammyboy289; Feb 8th, 2010 at 5:53 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sammyboy289 is offline Offline
5 posts
since Feb 2010
Feb 8th, 2010
0
Re: Help with loops and when they end? for a newbie?
ok, i want to thank nezachem for showing why it went wrong, and i want to thankEne Uran as to showing me what was the way to fix it. I have finally sorted out my code, and i am very thankful to all of you for helping me. You have helped me understand and hopefully i can become better at python
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sammyboy289 is offline Offline
5 posts
since Feb 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Tkinter, PIL, PNG transparency issue
Next Thread in Python Forum Timeline: Resources on building custom importer.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC