Helpp | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Helpp

I have a case where I have a list called "bil" where it contains random numbers, the number of components of that number as much as the user requests. *For example, the user enters the number of components as many as 3, then fill the list as much as 3. bil = [3,9,20] Then I had to reverse the order of the "bil" list. I'm still new to python so i don't know how to solve it How do I solve this case?

27th Nov 2021, 2:32 PM
Glenn
Glenn - avatar
12 Answers
+ 10
Glenn , here are some hints: (1) from random module we can use the choices method. this allow us to get k numbers out of a range (2) input how many numbers should be generated (3) use choices with a range from 0 upto 51, and get 3 random numbers as a list. [::-1] reverses the list: res = choices(range(0,51),k=num_inp) # num_inp contains the number of list elementes that should be generated
27th Nov 2021, 6:09 PM
Lothar
Lothar - avatar
+ 3
Hint: 1.) import random and receive input data and convert to an integer 2.) initialize/ declare your list 3.) generate random nambers in a loop as many times as your input numbers an add in this list. Next time please link your attempt with a question.
27th Nov 2021, 2:44 PM
JaScript
JaScript - avatar
+ 2
Reversing part: You can use reverse() or [::-1] https://www.programiz.com/JUMP_LINK__&&__python__&&__JUMP_LINK-programming/methods/list/reverse To understand why [::-1] reverses your list you can read here about list slices: https://stackoverflow.com/questions/509211/understanding-slice-notation
27th Nov 2021, 2:41 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Okay everybody, thanks for helping me🙏
28th Nov 2021, 1:00 AM
Glenn
Glenn - avatar
+ 1
num=int(input()) bil=[3, 9, 20] print(bil[num])
27th Nov 2021, 2:40 PM
Sancho Godinho
Sancho Godinho - avatar
+ 1
JaScript how i make a random number ?
27th Nov 2021, 2:55 PM
Glenn
Glenn - avatar
+ 1
27th Nov 2021, 3:00 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
The solution is Ramprasad code.
27th Nov 2021, 4:49 PM
SoloProg
SoloProg - avatar
+ 1
import random user_input = int(input()) runs = 0 bil = [] while runs != user_input: bil.append(random.randint(0, 100)) runs += 1 reversed_bil = bil[::-1] print(reversed_bil)
28th Nov 2021, 3:38 PM
Nikk
Nikk - avatar
+ 1
You can use functions and methods, in this specific case, use: list.reverse()
29th Nov 2021, 1:16 PM
Esteban Torres Aráuz
Esteban Torres Aráuz - avatar
0
To get random numbers in python first u need import the random module.... Then use a while loop to get the desired amount of numbers...... This is exactly what this code does... import random bil = [] user = int(input()) m = 0 while True: bil.append(random.randint(0,999)) m += 1 if m == user: print(bil) break
27th Nov 2021, 2:42 PM
Ramprasad
Ramprasad - avatar
0
Esteban Torres Aráuz. oh, yeah, I forgot about that
30th Nov 2021, 1:01 PM
Nikk
Nikk - avatar