Hi yall this is my first post here and I am definitely a newbie to python. I am trying to Create a list that holds 10 random numbers in a range of 1 to 10 and use a while loop to only print the values in the list that are divisible by 3.
So far I made this code:
import random
list = []
randlist = random.sample(range(0, 10), 10)
list.append(randlist)
while len(list) > 0:
if (list[0] % 3 == 0):
print(list)
But I get the error
unsupported operand type(s) for %: 'list' and 'int'
How do I fix this code? Everything runs perfectly until the while loop.
CodePudding user response:
The problem is that you are trying to take the modulus of a list.
On line 4: list.append(randlist)
, you are appending a list of numbers, randList
into list
.
This means that list[0]
will simply return randList
. If we want to retrieve a number from randList
, we could do:
while len(list) > 0:
if (list[0][0] % 3 == 0):
print(list)
However, there is no need for list
now since we know randList
will store the 10 random numbers.
To find the numbers in randList
that are divisible by 3, we can iterate through the list with a loop and use an if statement:
for i in randlist:
if i % 3 == 0:
print(i)
If you truly want to use a while loop, we can create a counter variable, index
, and while index < len(list)
, we can check to see if the element of randList
at index, index
is divisible by 3. Make sure you increment the value of index
so we don't end up in an infinite loop!
index = 0
while index < len(randlist):
if randlist[index] % 3 == 0:
print(randlist[index])
index = 1
We can also use print(randlist)
just to make sure we iterated through the whole list and got all the multiples of 3.
I hope this helped answer your question! Please let me know if you need any further clarification or details :)
CodePudding user response:
Try this with list comprehension.
import random
[rand for rand in random.sample(range(0, 10), 10) if rand%3==0]
CodePudding user response:
It's because you have used list
in your code. Which makes it a nested list like[[1, 2, ..]]
.
Solution to your code is:
import random
randlist = random.sample(range(0, 10), 10)
length = len(randlist)
while length > 0:
num = randlist[length-1]
if (num % 3 == 0):
print(num)
length = length - 1
A simple solution of it to use a for loop instead of a while. You can do with for loop like
import random
randlist = random.sample(range(0, 10), 10)
for num in randlist:
if (num % 3 == 0):
print(num)
You don't need to reduce the index
in for loop.
The same can be done by list comprehensive.
import random
randlist = random.sample(range(0, 10), 10)
moduled_list = [num for num in randlist if num%3 ==0]
print(moduled_list)
CodePudding user response:
The problem is that you use the append() method and use randlist as argument. The result is that you create a list into another list. You can verify that by printing your list and look at the result you obtain. As the random.sample() method already creates the desired list you do not have to use the append() method here. Namely, after your list with random numbers has been created you can simply iterate over all numbers in this loop and check at each iteration if the number is divisible by three. Thus, I would recommend you to use a for loop instead of a while loop.
The following code prints all numbers of the random list that are divisible by three:
import random
list = random.sample(range(0, 10), 10)
for number in list:
if (number % 3 == 0):
print(number)
A second and more advanced method would be to use list comprehension that print the desired numbers as a list:
import random
print([number for number in random.sample(range(0, 10), 10) if number % 3 == 0])