Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a loop that shuts down when given answer in python?

what I am wanting to do is randomly generate two numbers that equal a given number. Yet to allow for this to get the desired answer I want it to be random. That is the problem.

a=(1,2,3,4,5,6,7,8,9,)
from random import choice
b=choice(a)
c=choice(b)
d= c+b
if d == 10:
#then run the rest of the program with the value's c and b in it
#probably something like sys.exit goes here but I am not sure to end \/
else:
# i have tryied a few things here but I am not sure what will loop it around*

(thanks for the help :D)

I have know created a list called 'right' and know trying to append values a and b to the list yet that is not working. For I am knowing running the program 'in for trail in range(100)' so I get the answer. Yet the values are not appending into the new list right. Which is the problem. Then what I am going to do it read values 0 and 1 in the list right and then use them.(sorry its not that well done for at school) This is for fractions not adding to a given variable. This bit second but is.

import sys
right=(0)
y=x+x
from trail in range(y)
a=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
from random import choice 
A=choice(a)
B=choice(a)
d=A/B
if d==1:
    right.append(A)
    right.append(B)
else:
    x.append(1)
like image 228
fanjojo Avatar asked May 27 '26 17:05

fanjojo


1 Answers

 from random import choice

 a = range(1, 10)
 b = c = 0
 while b + c != 10:
     b = choice(a)
     c = choice(a)  # You meant choice(a) here, right?

But this accomplishes the same thing:

 b = choice(a)
 c = 10 - b

For decimal numbers betweeen 0 and 10:

from random import uniform

b = uniform(0, 10)
c = 10 - b
like image 165
Lauritz V. Thaulow Avatar answered May 30 '26 05:05

Lauritz V. Thaulow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!