Search code examples
pythonoopmethodsreturn-type

Python - class object return value


Beginner Pythonista here

Making a betting game as part of an OOP exercise.

made a Player class with a bet function:

# Player Class
class Player():
    def __init__(self,name):
        self.name = name
        self.hand = []
        self.chips = 0
    
    def deposit(self,amount):
        self.chips += int(amount)
        print(f"{amount} chips deposited.\nTotal available funds: {self.chips}")
        
    def bet(self):
        amount = int(input("Place your bet: "))
        if not amount > self.chips:
            self.chips -= amount
            print(f"You bet {amount}. New Balance: {self.chips}")
            return amount
        else:
            print("You cannot bet above your chips")

I was under the impression that a method within a class acted as a function, but the "return amount" command returns "None/NoneType".

How can I return the integer value to add to assign to a variable?

Thanks in advance


Solution

  • Probably went through else statement, that does not return anything. Tried your code and it works as expected if you meet if criteria in bet function (one that has return)

    a.chips = 100
    a.bet()
    Place your bet: >? 1
    You bet 1. New Balance: 99
    Out[8]: 1