Isn’t this list comprehension from solution redundant?
random_list = [random.randint(1,101) for i in range(101)]
Firt part is already saying that we want a random number in range from 1 to 101. Why do we need the last part saying the same thing again? When I tried to change it to any number, even 1, I still got random numbers from 1 to 101.
What do you argue it should be instead?
What is the purpose of it that it’s supposed to implement?
Would you still be satisfying that purpose with your proposed alternative, after removing whatever you are arguing is redundant?
I have a feeling that we put it there only because list comprehension is like [X for Y in Z] so it just has to have some Z. If I remove last part I get syntax error. But when I put ANY >0 number in there it works just fine, doesn’t matter if it’s 1 or 100000.
What do you mean by “just fine”?
You have to consider what the purpose is.
What does that part of a list comprehension do, what effect would changing it therefore do?
Why is list comprehension used at all?
Anyway I have no clue what it’s supposed to do, so I couldn’t possibly tell whether it’s doing something redundant, but it does do things and changing inputs to it will have effects in the result and whether that matters comes down to the purpose of it all… (and again, you should be considering the intentions before you consider the code) But what the effect of changing it is, well, ask yourself what that part of list comprehension does, you need to know that to reason about list comprehension anyway, you can’t begin to say it’s redundant before you know what it does
0 is still there.
Anyway, after looking at the exercise, what’s redundant is in the idea that is implemented.
…so consider the intention before considering the code
the redundancy is in the intention
If you create a list of random numbers
And then choose a random value from that list.
Then your result is equivalent to generating a single random value, the list is therefore redundant.
What does that have to do with list comprehension? It doesn’t, it’s the whole plan/concept/idea/intention that’s bad, and that’s the thing to reason about, not list comprehension.
We are supposed to make a list that provides us with 100 different numbers between 1 and 100.
Using the code provided from the solution:
random_list = [random.randint(1,101) for i in range(101)]
if I print the random_list I do see the number 101 showing up amongst the 100 numbers.
So my question is:
Does the argument in random.randint() not work the same as in range()?
By that, I mean how the first argument is inclusive while the second argument is exclusive.
If I change the second argument in random.randint() from 101 to 100 I only observe numbers between 1 and 100.
yeah you’re correct because in the ques it is said to generate a random integer between 1 and 100 (inclusive) for each number in range(101) mean it should always generate random numbers between 1 and 100 but in my case where I’ve assumed x to be variable it is generating random numbers between 1 and x. Strange thing is that my solution was still accepted because I guess the question was looking more for answer than solution. Thank you for correcting me.
On this page, Codecademy asks us to do the following:
1. In script.py import the random library.
2. Create a variable random_list and set it equal to an empty list
3. Turn the empty list into a list comprehension that uses random.randint() to generate a random integer between 1 and 100 (inclusive) for each number in range(101) .
4. Create a new variable randomer_number and set it equal to random.choice() with random_list as an argument.
5. Print randomer_number out to see what number was picked!
I have the following solution, which uses list comprehension, but I don’t understand why this is needed at all compared to another solution I came up with (commented below my solution). Could someone explain what’s the point of using a list?
# Import random below:
import random
# Create random_list below:
random_list = []
# Create randomer_number below:
for i in range(101):
random_list.append(random.randint(1,101))
randomer_number = random.choice(random_list)
# Print randomer_number below:
print(randomer_number)
# Why not use this? It's much shorter.
# It seems to do the same thing, unless I'm mistaken.
#import random
#print(random.randint(1,101))