I am using Python.
How can I make a code that generates random numbers between 1 and 1000 (inclusive) infinitely until it generates the number 39 and stops?
Here is what I've tried so far, however it has not worked:
import random
print(random.randint(1,1000))
if random.randint=39:
break
Try this code:
import random
while random.randint(1,1000) != 39:
pass
Or (depending on your needs):
import random
while True:
if random.randint(1,1000) == 39:
break
If you want to create generator try something like this :
import random
def generate_numbers(exact_number):
while True:
n = random.randint(1,1000)
if n == exact_number:
break
yield n
#example usage
for i in generate_numbers(39):
print(i)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.