X
X
xxx2017-02-15 01:46:53
Python
xxx, 2017-02-15 01:46:53

Recursion in python using random as an example?

import random 
def rand():
  a =random.randint(1,2)
  return a

The function returns either 1 or 2, each time differently. You need to write a loop with recursion with the condition, if a == 2, return 2 and end the loop, if 1, then run the loop again until there is 2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
anton_lazarev, 2017-02-15
@Heavy10110

Not strong in Python:

import random 
def rand():
  a = random.randint(1,2)
  while (a != 2):
    a = rand()
  return a

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question