Reputation: 105
I am a Newbie currently taking an online python course.
# Generate Number 1
from random import choices
population = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
weights = [0.11, 0.10, 0.14, 0.09, 0.13, 0.03, 0.08, 0.07, 0.09, 0.02, 0.03]
number1 = choices(population, weights)
# Generate Number 2
population2 = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
weights2 = [0.03, 0.07, 0.05, 0.05, 0.09, 0.04, 0.06, 0.07, 0.02, 0.04, 0.04, 0.05, 0.03, 0.05, 0.01, 0.04]
number2 = choices(population2, weights2)
if number1[0] + 1 < number2[0] < number1[0] + 12 :
print(number1 + number2)
else :
I was able to generate 2 random numbers with their respective weights with the help of some posts here on this website.
If the condition on the if
statement is not true i want to tell the program to keep generating number 2 again until such condition is met. My problem is that i don't know what to look for to study.
Could someone please provide me a hint for what i have to study? Such as " look into For
loops or into x
python function". I hope i can study and figure it out by myself rather than having someone tell me what to do directly.
would this post be of aid in my case?
Generate random number again if it doesn't equal x
using 2020.3 Community Edition PyCharm
Thanks for your help and time.
Upvotes: 0
Views: 414
Reputation: 188
one way is you could add while
loop in else
# Generate Number 1
from random import choices
population = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
weights = [0.11, 0.10, 0.14, 0.09, 0.13, 0.03, 0.08, 0.07, 0.09, 0.02, 0.03]
number1 = choices(population, weights)
# Generate Number 2
population2 = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
weights2 = [0.03, 0.07, 0.05, 0.05, 0.09, 0.04, 0.06, 0.07, 0.02, 0.04, 0.04, 0.05, 0.03, 0.05, 0.01, 0.04]
number2 = choices(population2, weights2)
if number1[0] + 1 < number2[0] < number1[0] + 12 :
print(number1 + number2)
else :
while(!(number1[0] + 1 < number2[0] < number1[0] + 12)):
number2 = choices(population2, weights2)
but the problem is that condition inside while
should satisfy for some value otherwise it would go in infinite loop.
Upvotes: 1
Reputation: 6090
Well, I would say one way to learn is to get a code you have no clue what it does, excepted that the output is what you want, and so you try to break it part by part to understand what that means.
Maybe after you can read. But reading about things you don't know is often not efficient.
So I am not explaining my answer, in line with what I've just written ;)
from random import choices
population = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
weights = [0.11, 0.10, 0.14, 0.09, 0.13, 0.03, 0.08, 0.07, 0.09, 0.02, 0.03]
population2 = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
weights2 = [0.03, 0.07, 0.05, 0.05, 0.09, 0.04, 0.06, 0.07, 0.02, 0.04, 0.04, 0.05, 0.03, 0.05, 0.01, 0.04]
while True:
number1 = choices(population, weights)
number2 = choices(population2, weights2)
if number1[0] + 1 < number2[0] < number1[0] + 12 :
print (number1 + number2)
break
else:
pass
Upvotes: 2