How to add custom parameter parameter in python functions?

60
August 10, 2018, at 2:40 PM

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.

Answer 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):
Answer 2
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.

Answer 3

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)
READ ALSO
Wagtail: Creating Recurring Events from Event page

Wagtail: Creating Recurring Events from Event page

I am creating a Wagtail site with an Events componentSome of these events are recurring, with the only difference being the start and end dates/times

39
How to filter list lines of a dictionary of lists according to a specific number?

How to filter list lines of a dictionary of lists according to a specific number?

I have a huge dictionary of lists that needs filteringHere's an example of its output:

36
Running scipy curve_fit produces runtime error maxfev = 1000

Running scipy curve_fit produces runtime error maxfev = 1000

I am running a curve fit in python that encountered the error, RuntimeError: Optimal parameters not found: Number of calls to function has reached maxfev = 1000Here is my code:

29
Rasterize Geometry Example not working

Rasterize Geometry Example not working

I am trying to recreate this rasterio example:

39