Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop help
#11
You indented both the j and i increments, you only wanted to indent the j increment. Unindent the line I += 1, so that it is part of the while I < 5: loop, but not part of the while J < 4: loop. And please don't post code like that. Just use python tags without formatting. See the BBCode tutorial in my signature below.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
Do you HAVE to use a while loop? Because this is just rewriting list_.sort() as an in-place sort.
Reply
#13
(May-08-2017, 04:41 PM)nilamo Wrote: Do you HAVE to use a while loop?  Because this is just rewriting list_.sort() as an in-place sort.
Homework, sir
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#14
(May-08-2017, 02:20 AM)jhall710 Wrote: I keep getting the same output [4, 5, 2, 6, 8] instead of [2, 4, 5, 6, 8]

here's my code

list_ = [6,8,4,5,2]
I = 0
J = 999
while I < 5:
   J = 0
   while J < 4:
       list_[J], list_[J + 1] = list_[J + 1], list_[J]  # This if statement will check values in list and then...
       J += 1
       I += 1
print(list_)

I get the output desired output keeping the IF statement....which I see you removed. The purpose of the If statement is to check the values. Your code does no checking of the values, its just random swapping, which is why you don't get the desired result. Cool  Below I have attached an example

list_ = [6,8,4,5,2]
I = 0
J = 999
while I < 5:

    J = 0
    while J < 4:
        if list_[J]> list_[J+1]:    # This if statement will check values in list and then...
            list_[J],list_[J+1] = list_[J+1],list_[J]# swap the values if the item after it has a lesser value
            J += 2                  # it should skip over the swap it just made.
           

        else:
            J += 1                     # keep moving forward 1
            I += 1                     # so the loop doesn't continue forever

print(list_)
Reply
#15
(May-08-2017, 08:59 PM)smbx33 Wrote:
       list_[J], list_[J + 1] = list_[J + 1], list_[J]

Just an idle observation - not for OP, it's slightly advanced! - the line above may be re-written as
       list_[J:J+2] = list_[J+1],list_[J]
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#16
list_[J:J+2] = list[J+1:J-1:-1]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020