#πŸ”’ TypeError: HumanPlayer.options() takes 0 positional arguments but 1 was given

238 messages Β· Page 1 of 1 (latest)

tardy trout
#

homework project to make a top trumps game using oop, made a computer player and human player class.
for the main i did while len(humanplayer.deck) > 0 or len(computerplayer.deck) > 0: to keep the game running as long as either class still has cards but i get that error, never encountered smth like that before

austere ferryBOT
#

@tardy trout

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wary ice
#

!e ```py
print(type(list[str]))

austere ferryBOT
tardy trout
#

class Player:
def init(self,deck):
self.deck = list[deck]
def addcardtoback(newCard,wondeck,lostdeck):
woncard = wondeck[0]
woncard.append(wondeck)
wondeck.pop[0]
newCard.append(wondeck)
lostdeck.pop[0]

austere ferryBOT
#

Hey @tardy trout!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
wary ice
#

see

tardy trout
#

must be related to this then

#

human and computer class inherit the player class

#

i think i get some sort of error if i use square brackets for the self list though

#

gimme a second

wary ice
#

you're setting it to be list[T] (T being some object)

wary ice
tardy trout
#

were u casting or uhhh

#

sry im kinda new

wary ice
tardy trout
#

what would u change

wary ice
austere ferryBOT
wary ice
#

see

#

the type of list[str] is types.GenericAlias

#

list[str] isn't a built-in list - it's a types.GenericAlias object

tardy trout
#

sorta following

blazing dawn
#

i still wonder how GenericAlias knows what type the original has

wary ice
# tardy trout what would u change
  • whitespace between functions
  • whitespace between arguments
  • consistent naming conventions for arguments (seriously tho - what is newCard, wondeck and lostdeck? what are these names?)
  • preferably some typehints as annotations to make it easier for people reading your code to know the types of each variable
wary ice
austere ferryBOT
#

class types.GenericAlias(t_origin, t_args)```
The type of [parameterized generics](https://docs.python.org/3/library/stdtypes.html#types-genericalias) such as `list[int]`.

`t_origin` should be a non-parameterized generic class, such as `list`, `tuple` or `dict`. `t_args` should be a [`tuple`](https://docs.python.org/3/library/stdtypes.html#tuple) (possibly of length 1) of types which parameterize `t_origin`...
blazing dawn
wary ice
#

you instantiate it WITH the original type, plus its args

tardy trout
#

again im relatively new so bear wit me sorry lol

blazing dawn
#

just space <-

tardy trout
#

ok

wary ice
#

!e ```py
from types import GenericAlias

class Collection:
def class_getitem(cls, T: type) -> None:
return GenericAlias(cls, (T,))

collection = Collection[int]

print(collection, type(collection))

austere ferryBOT
wary ice
#

ah shit, that should be -> GenericAlias, not -> None

blazing dawn
#

gg

wary ice
#

instinct

wary ice
wary ice
#

basically invisible characters

blazing dawn
#

also \n and \t

tardy trout
#

alright

wary ice
tardy trout
#

in terms of the variable names ig theyd only make sense in terms of the context but yeah they could be named better

#

for sure

blazing dawn
tardy trout
#

TypeError: HumanPlayer.options() takes 0 positional arguments but 1 was given

wary ice
tardy trout
#

uhhh hold up

dire pecan
wary ice
blazing dawn
wary ice
#

this was

dire pecan
#

well that is why they got a GenericAlias

tardy trout
#

this is a new one im getting

dire pecan
#

it should've been list(...)

tardy trout
#

like just now after changing the list thingy

tardy trout
#

yup

dire pecan
#

!paste

austere ferryBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

dire pecan
#

can you put the full, current code here?

tardy trout
#

ok

#

dont judge plsss

dire pecan
#

(then paste the resultant link)

tardy trout
#

give criticism tho

#

its not that long

#

@blazing dawn ^

#

oh yeah right the project involves us creating objects

#

from a text file that our teacher gave us

#

we read the lines

#

of the text file

#

and make card objects

wary ice
tardy trout
#

sure

blazing dawn
#

omg its awful sorry

#

please use a formatter

wary ice
# tardy trout sure
class Card:

    def __init__(self,title,price,ram,clockspeed,year,lifespan,popularity):

        self.title = str(title)

        self.price = float(price)

        self.ram = int(ram)

        self.clockspeed = float(clockspeed)

        self.year = float(year)
    
        self.lifespan = float(lifespan)

        self.popularity = float(popularity)

probably use a dataclass

#

!d dataclasses.dataclass

austere ferryBOT
#

@dataclasses.dataclass(*, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False)```
This function is a [decorator](https://docs.python.org/3/glossary.html#term-decorator) that is used to add generated [special methods](https://docs.python.org/3/glossary.html#term-special-method) to classes, as described below.

The `@dataclass` decorator examines the class to find `field`s. A `field` is defined as a class variable that has a [type annotation](https://docs.python.org/3/glossary.html#term-variable-annotation). With two exceptions described below, nothing in `@dataclass` examines the type specified in the variable annotation.

The order of the fields in all of the generated methods is the order in which they appear in the class definition.
tardy trout
#

TypeError: HumanPlayer.options() takes 0 positional arguments but 1 was given

blazing dawn
#

please use a formatter πŸ™
try

#

!pypi black

austere ferryBOT
#

The uncompromising code formatter.

Released on <t:1728328850:D>.

blazing dawn
#

or ruff

#

!pypi ruff

austere ferryBOT
#

An extremely fast Python linter and code formatter, written in Rust.

Released on <t:1736535473:D>.

dire pecan
wary ice
#
@dataclass
class Card:
    title: str
    price: float
    ram: int
    clock_speed: float
    year_released: float
    lifespan: float
    popularity: float
blazing dawn
#

follow PEP8 or die /j

wary ice
tardy trout
#

i wanna fix all the current errors before i work on optimising the program

#

ill look into the formatting stuff and decorators tho

#

our teacher is pretty shit so everything ive done ive had to research

#

doesnt tell us anything

wary ice
#

what the hell happened here

blazing dawn
#

ewwwwwwww

small maple
#

if you use a decent IDE, you usually see hints like these:

blazing dawn
dire pecan
#

let's focus on helping them instead of roasting them

tardy trout
blazing dawn
#

were not roasting them

wary ice
blazing dawn
#

its just a lot of new comers dont really do anything about formatting

wary ice
#

what it's trying to do is do HumanPlayer.options(instance)

blazing dawn
#

and i think its okay to guide them to use them

wary ice
#

this doesn't work because the arguments are empty

#

self is always the first argument in a method

#

!e ```py
class A:
def f(n: int) -> int:
return n

print(A.f(5))
print(A().f(5))

austere ferryBOT
blazing dawn
#

gg

wary ice
#

see

tardy trout
wary ice
tardy trout
#

or is that the same thing

wary ice
wary ice
#

that's what that error means

blazing dawn
#

class and instance methods / attrs are super different

tardy trout
#

alr

blazing dawn
#

make it classmethod or staticmethod

small maple
#

There are several issues with the usage of classes and functions/methods here... the methods are basically just functions, and they access the global variables computerplayer and humanplayer ..
Those "methods" assign to humanstat etc. which are not global, but used outside (at the bottom) as if they're meant to be globals.

blazing dawn
#

they should attr to self

tardy trout
#

the class

blazing dawn
#

if you dont plan on sharing data across functions then yes

#

like this is the way to eliminate globals

#

theyre jucky

small maple
#

the problem is, that you can't change humanstat without making it global - which is bad practise.. or imho you could just get rid of classes, and simply use arguments and return values with those functions

tardy trout
#

that would be nice but our teacher specified that we have to make classes

dire pecan
#

why are you here @wide ruin

#

please don't troll in people's help posts

wide ruin
#

hes my boyfriend dude..

tardy trout
#

i know nothing abt methods

#

so basically

#

if i put the decorator @classmethod

#

above my function

#

does that change something

small maple
#

a method is a function in a class, that has "self" as first argument... staticmethod and classmethod are special kind of methods, and I don't think you should use them, if you don't understand them yet. Your teacher would 100% ask you to explain them if you did πŸ˜‰

wary ice
small maple
#

just stick to the basic rule for now: every single function/method in your class should have "self" as first parameter.

blazing dawn
tardy trout
#

thank u bro

#

NameError: name 'humanstat' is not defined

#

prolly cuz i defined it within the switch statement

#

what should i call it outside

#

humanstat = " " ?

#

actually idk

small maple
#

that won't work. the humanstat inside of the function is a different variable from the humanstat outside of it

tardy trout
#

oh wait did i make 2

small maple
#

!e

myvar = 123

def change_myvar():
    myvar = 456

change_myvar()
print(myvar)
austere ferryBOT
small maple
#

changing a variable in a function doesn't actually affect the variable outside of it

#

unless you declare it to be a global variable inside of that function, but that is bad practise

#

the right way would usually to return the value instead

blazing dawn
small maple
#

!e

myvar = 123

def change_myvar():
    myvar = 456
    return myvar

myvar = change_myvar()  # this becomes whatever is returned
print(myvar)
austere ferryBOT
wary ice
tardy trout
#

thats jus what our school computers usin

wary ice
wary ice
tardy trout
#

AttributeError: 'HumanPlayer' object has no attribute 'options'

#

happens when its the players turn

blazing dawn
#

a method or an attribute?

#

also resend code

#

!paste

austere ferryBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

tardy trout
hard haven
#

it looks like the options method is defined within your init, so it's contained to the scope of the init function. it's not part of the class, as such, but rather to that specific function. Unindent the definition for the options function to same indentation level as the init.

tardy trout
#

yep that was right

#

i love indents

hard haven
#

you got it to work how you expected it to?

tardy trout
#

NameError: name 'humanstat' is not defined

#

gonna read over what sani said again

#

cuz im not 100% sure on it

#

i think everything else in theory should work after that but uh

#

who knows

hard haven
#

if you're still stuck then ping me and I'll see what i can do with the code you have so far.

tardy trout
#

@hard haven how would i put what sani said into practice in terms of my code

#

sry a bit slow lol

hard haven
#

no worries, let me go read it quickly

#

what does your code look like at the moment?

#

@tardy trout

tardy trout
#

@hard haven ^

hard haven
#

Now reading through it, I'll start by creating the global variables in the program, modifying the class to interact with these, and going from there

tardy trout
#

thats what i was thinking but whys that bad practice like sani said

hard haven
#

It's usually because if you try to have multiple classes using global variables with the same name, they will affect each other

#

e.g. If both wanted to use a global variable called stat, it would be the same variable in the eyes of the interpreter, and they will change the same variable and cause stuff to not be cool

tardy trout
#

damn that sounds long

hard haven
#

it kinda was but its alr

#

I think it should work, but can you give me a cards.txt to use just to test it?

tardy trout
#

sure thing

#

should i dm u it

#

cant send files

hard haven
#

yh sure

#

what does the skip variable do within your program?

tardy trout
#

when the player wins they can choose their card again

#

i mean

#

the category/stat

#

goes for both human and computer

hard haven
#

I think I'm a lot closer, but still has errors. I'll paste the edited version in here and if you have any worries about it then let me know

#

This is what I have done so far, but haven't had enough time to fix the other errors occurring.

tardy trout
#

appreciate it

#

will look over it

#

whats dict

hard haven
#

its a dunder (double underscore) method that returns a dictionary form of the card.

#

i'll have to add it in later, but its the dictionary equivalent of __str__

tardy trout
#

ill research that

hard haven
#

I'm gonna go to sleep soon because it's getting late for me, so if you ping me I won't see it until like 8am GMT. Still ping me tho if you have any issues so it prioritises over other dc notifications for me

tardy trout
#

no problem dude u been a massive help

#

gn

hard haven
#

❀️

austere ferryBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.