Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop help
#1
Hey everyone, have all of my code correct thus far (so I think lol) but steps 6 & 7 have me very lost. My instructor never gave us any information on swaps and I'm thinking there is to be a loop within a loop but i'm not sure if that is even possible with python. Appreciate any help/direction
  1. Draw five adjacent boxes and number the boxes 0 though 4

  2. Put the following numbers in boxes 0 - 4, one number per box: 6 8 4 5 2
  1. Draw a box, label the box I, write the number 0 in box I

  2. Draw a box, label the box J, write the number 999 in box J

  3. If the number in box I is less than 5 then do the next instruction, otherwise do instruction 12

  4. Replace the number in box J with 0

  5. If the number in box J is less than 4 then do the next instruction, otherwise do instruction 10

  6. If the number in the numbered box whose box number is the same as the number in box J is greater than the number in the box that follows then do the next instruction, otherwise do instruction 8

  7. Swap the value in the box whose box number is the same as the number in box J with the value in the box that follows

  8. Replace the number in box J with the number that is one more than the number currently in box J

  9. Go back and do instruction 5 again

  10. Replace the number in box I with the number that is one more than the number currently in box I

  11. Go back and do instruction 3 again

  12. You are done

list_ = [6,8,4,5,2]
I = 0
J = 999
while I < 5:
  J = 0
  while J < 4:
    
    
  J = J + 1
  I = I + 1
Reply
#2
Since your instructions use the word 'draw' with boxes, I have to assume that you're expected to use a GUI.
You have none in your code.
Which GUI's did you cover in class?
Reply
#3
Thats just in there because we were to draw out the algorithm on paper and now the second portion is to code it
Reply
#4
Then be clear, how do you want the output to look (example please)
Reply
#5
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 += 1
           I += 1
       else:
            J += 1
            I += 1
print(list_)
Without knowing exactly what the output should be I won't know if its correct, but judging by the steps you are stuck at, it looks like it wants you to swap values in a list.
Reply
#6
the output should be [2, 4, 5, 6, 8]
Reply
#7
Then you need to indent line n, so it is under the j loop; and write step 7 on line 7 with the same indentation (see smbx33's line 8).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
hmm still confused by what you mean. what do you mean by line n?
Reply
#9
That was a typo, I meant line 9.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#10
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_)
Reply


Forum Jump:

User Panel Messages

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