0

I created a simple 'stone paper scissors game' in python with oops. why python gives None in the last line of output?

class Game:
    def gameWin(self,comp,user):
        if comp== user:
            print('Match is Tie!')
        elif comp=='s':
            if user=='p':
                print('You won!')
            elif user=='c':
                print('You Lose!')
        elif comp=='p':
           if user=='c':
               print('You won!')
           elif user=='s':
               print('You Lose!')
        elif comp=='c':
           if user=='s':
               print('You won!')
           elif user=='p':
               print('You Lose!')
list=['s','p','c']
comp= random.choice(list)
print('computer turn......')
object=Game()
user=input(('Your turn: select any of one [s,p,c]: '))
print(f'computer chose {comp}')
print(object.gameWin(comp,user))  

output:

computer turn......
Your turn: select any of one [s,p,c]: c
computer chose p
You won!
None

why python gives None in the last line?

L3viathan
  • 26,748
  • 2
  • 58
  • 81
  • 3
    Does this answer your question? [Function returns None without return statement](https://stackoverflow.com/questions/7053652/function-returns-none-without-return-statement) – Brian61354270 Oct 17 '20 at 14:25
  • Read up on the difference between `return` and `print`. You're printing what your method returns, which is `None`. – L3viathan Oct 17 '20 at 14:27
  • If it doesn't, `print(object.gameWin(comp,user))` should be `object.gameWin(comp,user)` – Elliott Frisch Oct 17 '20 at 14:28
  • Welcome to SO. If you are just learning to program or new to Python it is very likely that any question you have or stumbling block you encounter has been experienced before and asked about here on SO. If you spend a little time honing your search skills you can probably find answers to many of those questions. – wwii Oct 17 '20 at 14:31

1 Answers1

0

It's because you're printing object.gameWin(comp,user). Since object.gameWin(comp,user) doesn't return a value, it prints None. Just remove the print around that last line. If you want to learn more about returning values, you can look at this question: What is the purpose of the return statement?

Tabulate
  • 611
  • 6
  • 19