Hi, I am a beginner for Python. There is one problem I encounter with a coin flip program which there are "None" involved with the print out. Any help will be appreciated.

import random

def flip():
    cflip = random.randrange(2)
    if cflip == 0:
        print("Heads")
    else:
        print("Tails")
     
def main ():
    for x in range (10):
        print (flip())
main ()

Here is the answer:

>>> 
Heads
None
Tails
None
Tails
None
Tails
None
Heads
None
Heads
None
Heads
None
Tails
None
Heads
None
Tails
None
>>>

Like I mentioned, there are strange "None" appear for each time the function is called. Please help. Thanks for your time.

Recommended Answers

All 2 Replies

You should return value from function, not print.

@pyTony Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.