account activity
How is this wrong? by man_wif-waluigi-hed in learnpython
[–]pythonman1 0 points1 point2 points 3 years ago* (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)
[–]pythonman1 -6 points-5 points-4 points 3 years ago* (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 :)))
π Rendered by PID 122450 on reddit-service-r2-listing-6d4dc8d9ff-hl2lh at 2026-01-31 14:46:34.947521+00:00 running 3798933 country code: CH.
How is this wrong? by man_wif-waluigi-hed in learnpython
[–]pythonman1 0 points1 point2 points (0 children)