1
import random

def diceroll():
    x = random.randrange(1, 6)
    print(x)
    repeatroll()

def repeatRoll():
    roll = input(print("Would you like to roll again?"))

    if roll.upper()=="Y":
        return diceroll()
    elif roll.upper()=="N":
        return print("You have chosen not to roll again")
    else:
        return print("Invalid input")

    return roll



repeatRoll()

Can anyone explain to me why this returns None after the code asks for an input?

I always thought that it would be due to not having a return function.

I'm completely confused, I feel like the answer is obvious but i'm not quite getting it.

Help would be appreciated, thank you.

imanidiot
  • 37
  • 4
  • 3
    ``diceroll`` does not return anything. It should probably ``return x`` or ``return repeatroll()``. – MisterMiyagi Jun 08 '20 at 17:40
  • 1
    Also, `print` returns `None`. You don't need to call `print` in order to return a value. – chepner Jun 08 '20 at 17:40
  • 1
    So the reason why 'None' appears in the output is because I am using a print statement for the 'roll' variable? – imanidiot Jun 08 '20 at 17:45
  • Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none) – MisterMiyagi Jun 08 '20 at 17:46

3 Answers3

2

print() always return None

It doesn't return any value; returns None.

Since you called print() inside input().The print() returns none which is take as input by input()

Use :

print("Would you like to roll again?")
roll = input()
Maghil vannan
  • 435
  • 2
  • 6
  • 19
0

It can be because print statements do not return anything in python or simply saying print statements have a None type as return.

0

Your code required small modifications:

Try using this snippet

Calling repeatRoll() inside diceroll() wont work because it is not in the same scope. And your inputting format was also invalid

import random

def repeatRoll():
    roll='Y'
    while(roll=='Y'):
        print("Would you like to roll again?")
        roll = input()
        if roll.upper()=="Y":
            diceroll()
        elif roll.upper()=="N":
            return print("You have chosen not to roll again")
        else:
            return print("Invalid input")


def diceroll():
    x = random.randrange(1, 6)
    print(x)


repeatRoll()

Hope it helps:")

Srivatsav Raghu
  • 399
  • 4
  • 11