0

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

0

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

Your code seems right except for the else condition. You should add to else condition in the method bet

return 0

or raise an error

raise ValueError('You cannot bet above your chips')

then you should handle it wherever you want to use it.

Comments

1

There is an indentation Error for me. The If statement in the bet method shouldnt be indented. After fixing that it works fine for me.

Creating an object from class Player:

test_player = Player("TestName")

Giving some deposit:

test_player.deposit(10)

10 chips deposited.
Total available funds: 10

And finally the betting:

test_player.bet()
Place your bet: >? 5
You bet 5. New Balance: 5

The last bet gives:

test_player.bet()
Place your bet: >? 6
You cannot bet above your chips

1 Comment

Sorry about the indentation error. It's an error from writing the code here. It's not in the original code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.