mardi 18 février 2020

Random populations of empty listes

i'm new to programming and i have a task in which i have to write a program in Python in which i give a selecting range number and a number of selections to append for 2 empty lists, so the approach of this program should be like this:

give range to select from : 10

give number of selections: 5

i should get:

list of numbers selected randomly: [2,3,7,1,8]

List of numbers not selected: [4,6,5,9,10]

NB: numbers should not be repeated nor exist in both lists

here's my try

import random
selected_list=[]
not_selected=[]
selecting_number=int(input('give range to select from : '))
select_num=int(input('give number of selections: '))
if selecting_number<select_num:
    print('error')
elif selecting_number>=select_num:
    for x in range(select_num):
        selected_list.append(random.randint(1,select_num))
    print(f'List of numbers selected randomly: {selected_list}')
    if not_selected not in selected_list:
        for y in range(select_num):
            not_selected.append(random.randint(1,select_num))
    print(f'List of numbers not selected previously: {not_selected}')

and i get this:

list of numbers selected randomly: [6, 7, 7, 7, 7]

List of numbers not selected: [2, 4, 4, 8, 5]




Aucun commentaire:

Enregistrer un commentaire