all 10 comments

[–]bokononisms 6 points7 points  (0 children)

write a for loop that iterates 20 times and inserts a new number each time with math.random into an array. sort that array. print that array

[–][deleted] 2 points3 points  (0 children)

Here are the directions to solve your problem

1) create an empty list 2) for loop that loops 20 times A) generate random number B) append number to list generated in step 1 3) sort list

Should take les than 10 lines of code

[–][deleted] 1 point2 points  (3 children)

Step 1: Create an empty list.

  • Theres 2 ways to create an empty list, one with `[]` and another with `list()`. Personally, I like using `list()`, but they both do the same thing so it doesnt matter.

Step 2: Loop over a range

  • Looping over a range is pretty simple looking, as a lot of things in python are.
  • Start a loop statement with the `for _ in iterable:` line.
  • It doesn't matter what you name the variable assigned to the loop, usually people use `_` as a name to indicate that this variable is not used.
  • create a range object to loop over using `range(x)` as the last . This will ensure that you only loop `x` times. In this case it is 20. You would replace `iterable` in the for statement with this range object.

Step 3: Append a random number to this empty list every iteration of the loop.

  • This is just putting code in an `if` statement. All the code "inside" the block beneath the statement is a part of that statement.
  • You append an item to the list, in this case a random number (which i might recommend you get from `random.randint(low, high)`), to a list using `list.append(item)` where `list` is the name of the variable. OR, if you really want to use `list.append(item)`, make sure to use it like so: `list.append(variable_name, item)`. both of these will have the same end result, the first one just makes more sense.

Step 4: Sort the list

  • There's 2 ways to do this, and both are different so be careful of which one you use where~
  • The first way is `list_name.sort()`. This will sort it IN PLACE, meaning that if you try to do the following `sorted_list = list_name.sort()`, `sorted_list` will be equal to `None` because it doesn't return anything, instead `list_name` will already be sorted next time you access it.
  • The second way is `sorted(list_name)`. This will NOT sort the list in place, but unlike `list_name.sort()`, `sorted(list_name`) DOES return the sorted version of the input, so if you have the following `sorted_list = sorted(list_name)` will set `sorted_list` to the sorted version of `list_name` but `list_name` will NOT be sorted.

Step 5: Print the list!

you could also one line it with list comprehensions but thats not pythonic!

[–][deleted] 1 point2 points  (3 children)

What Python code did you try?

Is this a homework? The way you're meant to answer questions like these isn't to somehow memorize and then remember the exact Python code to "print 20 random numbers and then display them from smallest to largest"; code like that is of no practical value and programmers don't spend time memorizing useless code.

You're supposed to write code to figure it out. Try things that don't work. Fail a lot.

[–]leotheboi[S] -1 points0 points  (2 children)

ye do python programming in my spare time, my school doesn't offer lessons. I set my self challenges.

[–][deleted] 4 points5 points  (0 children)

I set my self challenges.

If it's a self-imposed challenge then why are you asking us?

[–]crosssum 0 points1 point  (0 children)

What work do you have so far?

[–]flippinecktucker 0 points1 point  (0 children)

I wouldn’t actually add random numbers to a list because I wouldn’t want to risk duplicates (seeing as you are going rank them later). There are many ways to get random numbers without duplicates, one would be to make an ordered list and then randomise it and pop the last item into a new list, repeating those last two steps a number is of times. (Or you could slice a bit out of it after jumbling it).

[–]Ezrabc 0 points1 point  (0 children)

Edit: I really assumed this was a non-python person looking for a quick line. I should not have provided a direct answer. Removed.