I wrote a function with x value which can take either 1 or 2. And depending on x, I created another function inside this function, in which it checks the x value. When x == 1, then he makes return 1, and when x == 2, then return 0. Question I want to check the function for example if the function is exactly 1 then do something, otherwise do something else.

import random def coin(x=random.randint(1,2)): if x == 1 : print('Head') else: print('Tail') def flip(): a=None if x == 1: a=1 elif x == 2: a=0 flip() 

type how to check return for example:

if coin () == 1: print (+)

Something like this . Or tell me how to do it right.

    3 answers 3

    Just return the value from the coin and from the flip :

     import random def coin(): x = random.randint(1, 2) if x == 1: print('Head') else: print('Tail') def flip(): a = None if x == 1: a = 1 elif x == 2: a = 0 return a return flip() for i in range(10): if coin() == 1: print('coin == 1') 

      People! If my eyes do not change, then the function

        def flip(): a = None if x == 1: a = 1 elif x == 2: a = 0 

      You can write shorter:

       def flip(): return x%2 

      well, or another option:

       def flip(): return 0 if x==2 else 1 

        I do not write to python, but probably I need to add "return a" to the flip function

         def flip(): a=None if x == 1: a=1 elif x == 2: a=0 return a