Matthew Carrera
Matthew Carrera

Reputation: 21

How to print numbers in a list that are less than a variable. Python

import random

Random_num=random.randint(0,10)

if Random_num == 1 or Random_num == 2 or Random_num == 3 or Random_num == 4 or Random_num == 5 or Random_num == 6 or Random_num == 7 or Random_num == 8 or Random_num == 9 or Random_num == 10:
    l.append(Random_num)

print(l)

print(Random_num)

I want to make the Random number that I generate between 1 and 10 recognize that it is 1,2,3,4,5,6,7,8,9,10 put all the numbers less than it in a list.

Like if the Random Number is 7 then I want to put all the numbers less than it in a list.

I am a beginner. Also I am specifically on Python 3.7

Upvotes: 1

Views: 5157

Answers (5)

james shaw
james shaw

Reputation: 7

import random
random_num=random.randint(0,10)
your_nums = list(range(10))
wanted_nums = []
for your_num in your_nums:
    if your_num < random_num:
        wanted_nums.append(your_num)
print(wanted_nums)

not sure if above codes meet your need.there may be a better one.but this is all i know yet

Upvotes: -1

Adam
Adam

Reputation: 4580

import random
n = random.randint(0,10)
i = 1
l = []
while i < n:
    l.append(i)
    i = i + 1
print(l)

Upvotes: 2

kevinkayaks
kevinkayaks

Reputation: 2726

random_num=random.randint(0,10)
list = []
list.extend(range(random_num))

This should do the trick.

range(random_num)

is an iterator of all numbers smaller than random_num.

list(range(random_num))

is a list of all numbers smaller than random_num.

You can use the extend method to impart 'list' with the elements of the iterator.

Upvotes: 2

Miguel
Miguel

Reputation: 1345

1) you create your number the way you did.
2) you do my_list = range(random_num) which creates a list from 0 until your random_num -1

for example, if random_num = 7,
list = [0, 1, 2, 3, 4, 5, 6]

Upvotes: 2

U13-Forward
U13-Forward

Reputation: 71600

Your question is not that clear but here is what you want:

import random
n=random.randint(0,10)
l=list(range(n))
print(l)

Upvotes: 1

Related Questions