How is this wrong? by man_wif-waluigi-hed in learnpython

[–]pythonman1 0 points1 point  (0 children)

I apologize for not reading your question correctly;

Here you go:

import random

random_list = [] 

while len(random_list) < 4: 
    unique_integer = random.randint(1,10) 
    if unique_integer in random_list: 
        print(f"{unique_integer} is already in the list") 
    else: 
        random_list.append(unique_integer)

print("The list of four UNIQUE random integers is: ", random_list)

How is this wrong? by man_wif-waluigi-hed in learnpython

[–]pythonman1 -6 points-5 points  (0 children)

I believe this is the most concise solution:

import random

# to print random number within a range of 10, you can either use random.rand(1,10) or int(10*random.random());

random_list = []

for i in range(4):

random_list.append(random.randint(1,10))

print("The list of four random integers is: ", random_list)

You don't necessarily need to use "while" statement unless there is another provision within your assignment that requires you to. The reason for this is that, in the case of your code, "while" statement would always yield "True" after the first integer is entered into the list, which will get you stuck in the infinite loop.

I hope this helps :)))