1

My name is Piyush and I have just started learning to code in python. Right now I am working on a project. And would really appreciate if anyone can help in adding customized parameters in functions. I am mentioning my code and the problem I am facing down below:

class Chips ():
    def __init__ (self, total):

        while True:
            try:
                total = int(input("How many chips as total?: ?"))

            except:
                print("Please enter an integer")

            else: 
                break 

        self.total = total 
        self.bet = 0      

    def win_bet (self):
        self.total = self.total + self.bet   

    def loose_bet (self):
        self.total = self.total - self.bet  

However, I can set total = 100 and can run the game but I want the user to be able to enter the total chips he/she wants to add.

I want the input(total) in the while loop to be as the argument while running the game. But I keep on getting this error:

--
TypeError                                 Traceback (most recent call last)
<ipython-input-9-b1b7b1c195f7> in <module>()
    367 
    368     # Set up the Player's chips
--> 369     player_chips = Chips()  # remember the default value is 100
    370 
    371     # Prompt the Player for their bet:

TypeError: __init__() missing 1 required positional argument: 'total'

Please Help!

Thanks for your attention to my request.

3
  • You need to pass total to Chips: Chips(total). Don't ask for input inside __init__. Instead, call int(input()) outside Chips and pass the result. Commented Aug 9, 2018 at 17:29
  • Also, instead of using the except else clause, a nicer way is to put the break right after the line that might except total = int(... in your case. Commented Aug 9, 2018 at 17:34
  • @PatrickHaugh Thanks for your help. I will try to do that. Commented Aug 9, 2018 at 17:37

3 Answers 3

1

Your class takes a parameter in its constructor, but you also read it from the input in your constructor.

I think you are confused in what you are trying to achieve here.

Option 1:

If the caller of your code (the code that constructs your class), can be modified and know the total at the instance creation time, just add the parameter in the constructor call.

total = 100
player_chips = Chips(total)

Option 2:

in case you can't modify the caller, (most likely from what I read), then that means you want to actually read the total from the input. Remove the argument from your constructor.

def __init__ (self):

instead of

def __init__(self, total):
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time and help. I will definitely try the way you suggested.
0

You should add method get_total_from_user and set default param in constructor

class Chips ():
    def __init__ (self, total=None):
        self.total = self.get_total_from_user() if not total else total
        self.bet = 0      

    def win_bet (self):
        self.total = self.total + self.bet   

    def loose_bet (self):
        self.total = self.total - self.bet

    def get_total_from_user(self):
        while True:
            try:
                return int(input("How many chips as total?: ?"))

            except:
                print("Please enter an integer")

            else: 
                break 

It allows you to get total from user

Chips()

Or you can set it via passing value

Chips(100)

4 Comments

Thanks for your help as suggested I did that as well. But now I am getting this error;
Welcome to BlackJack! How many chips would you like to put as total?199 How many chips you would like to bet?10
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-6db745b9b190> in <module>() 377
378 # Prompt the Player for their bet: --> 379 take_bet(player_chips) 380 381 # Show the cards: <ipython-input-2-6db745b9b190> in take_bet(chips) 242 243 else: --> 244 if chips.bet > chips.total: 245 print(f"Sorry you do not have enough chips. You have {chips.bet}chips in your account ") 246 TypeError: '>' not supported between instances of 'int' and 'NoneType'
0
c = Chips(100)

at the bottom of your code - no error. You override the value of total in the interactive constructor, but hey, you promised "init" you were sending it.

c = Chips()

works if you change your signature to:

def __init__ (self, total):

The interactive constructor seems like a very bad idea overall.

4 Comments

Thanks for your help and time. I do agree but as being new to this language I am learning everyday and try to change the style of my coding according to professional standards.
Apart, I would like to ask for your suggestion about good study material to learn python?
I would really appreciate if you can tell me good python book or course for beginners.
StackOverflow has strict rules about advocating for external resources, product, platform, publication, etc. . If you search for "learning python" or "python practice" you will find a good number of suggestions. Look for platforms or quizzes that offer layers of challenges. Play with easier problems until they get too easy and then move to harder ones. Most paths will be helpful and some more helpful than others, but you are the best judge for what helps you.

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.