Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
While loop help
#1
Hello, I am very new to python and I feel very stuck. Here is my homework problem followed by what I have tried:

##### Step 3:
##### Create 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)

lis = list()
count = 0
while lis[count] < 20:
   i = random.randint(0,10)
   print i
This follows with an error stating that the line "while lis[count] < 20:" index is out of range. I am guessing this means that I have not added anything to my list so it cannot be less than 20? Any help is greatly appreciated.

Thank you
Reply
#2
You are using count as an index, not as a counter
you want to use len instead:
lis = []
while len(lst) < 20:
   i = random.randint(1,10)
   lis.append(i)
   print(i)
since you're  not using the list for anything, a better way is:
for x in range(20):
   print(random.randint(1,10))
Reply
#3
Thank you Larz you are da bomb. Due to my level of frustration and the amount that you have helped I am going to ask another question.

Here are the instructions followed by my attempt at completing them:


##### Next step:
##### Create loop that removes an unlucky number from the list generated in the last step.
##### 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)

- I have chosen '1' for my unlucky number

##step 1 (correct, no problems here)
lis = []
while len(lis) < 20:
   i = random.randint(0,10)
   lis.append(i)
   print(i)

## step 2 (incorrect, need help here) 
print lis.count(1)
if i == 1: 
   lis.remove(i)


and this does not remove all instances of '1' but only the first instance.

Thank you python-forum I hope my question is clear enough to receive help.
Reply
#4
The remove method only removes one instance of the number, as you discovered. So you need to remove that number x times, where x is how many times the number is in the list. So you need to save the count you are getting, and loop that many times, removing the number.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Jun-07-2017, 08:45 PM)Element346 Wrote: The remove method only removes one instance of the number, as you discovered. So you need to remove that number x times, where x is how many times the number is in the list. So you need to save the count you are getting, and loop that many times, removing the number.

So I tried this:
r = lis.count(1)
lis.remove(1)*r
This gives me an error code that I cannot multiply ' integers' with 'NoneType'. I also tried putting int() around lis.remove(1) but this gives a similar result. How do you multiply an operation like 'remove'?
Reply
#6
(Jun-08-2017, 12:30 AM)Element346 Wrote: How do you multiply an operation like 'remove'?

If you want to repeat an operation like remove, you do a loop. Like your while loop. A for loop would be better in this case.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
[/quote]

If you want to repeat an operation like remove, you do a loop. Like your while loop. A for loop would be better in this case.
[/quote]

 
lis = []
while len(lis) < 20:
    i = random.randint(0,10)
    lis.append(i)
    
print lis.count(1)
for numbers in lis:
    lis.remove(1)
    print lis
This returns an error message stating: ValueError: list.remove(x): x not in list


I feel like I am close and I am very grateful for the help I have truly been staring at this for hours on end.

Thank you
Reply
#8
How many times do you need to remove 1? lis.count(1) times.

for numbers in range(lis.count(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