Python Forum
while loop help needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop help needed
#1
I am very new to Python (and any programming language for that matter) and have a 2 part assignment I am struggling with. Below are the instructions as written.


##### Step 1:
##### Create while loop that adds 20 random numbers between 0 and 10 to a list
##### Don't forget to create a counter and an empty list first
##### When the loop is complete, the full list should print (only at the end, not each time a number is added)

##### Step 2:
##### Create loop that removes an unlucky number from the list generated in step 3
##### IF the unlucky number is not in the list to begin with, the script should print a message that says this.
##### Otherwise, the script should print a message saying how many times the number is being removed from the list,
##### and then remove the number.
##### This will require a while loop, and the .count() method.
##### When the loop is complete, the updated list should print (only at the end, not each time a number is removed)


This may seem very basic to many, but for the complete novice like myself, I am clueless. Any tips? TIA
Reply
#2
What have you tried? We are willing to help, but we are not going to do your homework for you.
Post your best attempt at the code in code tags and ask specific questions. If you get error, post the full traceback in error tags.
Reply
#3
(Jun-02-2017, 07:00 PM)thorny5 Wrote: ##### Step 1:
##### Create while loop that adds 20 random numbers between 0 and 10 to a list
##### Don't forget to create a counter and an empty list first
##### When the loop is complete, the full list should print (only at the end, not each time a number is added)
The while loop, and how to incrementally build lists, is what's important here. So here's how to get a random number:
>>> import random
>>> random.randint(0, 10)
7
>>> [random.randint(0, 10) for _ in range(20)]
[3, 6, 5, 6, 9, 7, 4, 10, 2, 5, 1, 1, 8, 7, 3, 1, 8, 3, 5, 6]
(Jun-02-2017, 07:00 PM)thorny5 Wrote: ##### Step 2:
##### This will require a while loop, and the .count() method.
That's not true, you almost never are REQUIRED to use anything.
Reply


Forum Jump:

User Panel Messages

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