#π TypeError: HumanPlayer.options() takes 0 positional arguments but 1 was given
238 messages Β· Page 1 of 1 (latest)
@tardy trout
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.
types.GenericAlias is what you get when you do something like list[int] or set[str]
!e ```py
print(type(list[str]))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
<class 'types.GenericAlias'>
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]
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.
see
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
self.deck = list[deck]
you're setting it to be list[T] (T being some object)
i mean normal brackets sorry
yep
see what i did earlier? #1328089097583071343 message
can i also suggest you rewrite your code for this? it looks quite messy to be honest
what would u change
!e ```py
x = list[str]
x_type = type(x)
print(x_type)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
<class 'types.GenericAlias'>
see
the type of list[str] is types.GenericAlias
list[str] isn't a built-in list - it's a types.GenericAlias object
sorta following
i still wonder how GenericAlias knows what type the original has
- whitespace between functions
- whitespace between arguments
- consistent naming conventions for arguments (seriously tho - what is
newCard,wondeckandlostdeck? 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
!d types.GenericAlias
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`...
maybe use a formatter? ruff or black
you instantiate it WITH the original type, plus its args
when u say white space do u just mean gaps
again im relatively new so bear wit me sorry lol
just space <-
ok
!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))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
__main__.Collection[int] <class 'types.GenericAlias'>
ah shit, that should be -> GenericAlias, not -> None
gg
instinct
no problem
yep. whitespace is anything that isn't rendered
basically invisible characters
also \n and \t
alright
\r, \n, \t, \f, \v and
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
yas
TypeError: HumanPlayer.options() takes 0 positional arguments but 1 was given
instead of wondeck and lostdeck, you can instead have winning_deck and losing_deck
uhhh hold up
ya sounds way better
this was the issue, right?
not sure what this is related to
can you paste the entire traceback?
this was
well that is why they got a GenericAlias
am i allowed to talk about multiple errors
this is a new one im getting
it should've been list(...)
like just now after changing the list thingy
!paste
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.
can you put the full, current code here?
(then paste the resultant link)
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
yeah sure. if you want to, you can change your post name to match your current error
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
@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.
TypeError: HumanPlayer.options() takes 0 positional arguments but 1 was given
that won't be conducive to fixing their error tho
@dataclass
class Card:
title: str
price: float
ram: int
clock_speed: float
year_released: float
lifespan: float
popularity: float
follow PEP8 or die /j
it's worth pointing out
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
what the hell happened here
ewwwwwwww
if you use a decent IDE, you usually see hints like these:
while stat not in {1, 2, 3}
let's focus on helping them instead of roasting them
π
were not roasting them
ok your issue is not using @staticmethod
its just a lot of new comers dont really do anything about formatting
what it's trying to do is do HumanPlayer.options(instance)
and i think its okay to guide them to use them
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))
:x: Your 3.12 eval job has completed with return code 1.
001 | 5
002 | Traceback (most recent call last):
003 | File "/home/main.py", line 6, in <module>
004 | print(A().f(5))
005 | ^^^^^^^^
006 | TypeError: A.f() takes 1 positional argument but 2 were given
gg
see
i was tryna call upon a function in the class rather an instance though
because we call it on the class and not the object, it works perfectly fine because the object is what provides self
or is that the same thing
nope
you're calling upon a function through an instance instead of a class
that's what that error means
class and instance methods / attrs are super different
alr
make it classmethod or staticmethod
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.
they should attr to self
should i define them outside
the class
if you dont plan on sharing data across functions then yes
like this is the way to eliminate globals
theyre jucky
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
that would be nice but our teacher specified that we have to make classes
hes my boyfriend dude..
yo btw abt this
i know nothing abt methods
so basically
if i put the decorator @classmethod
above my function
does that change something
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 π
it's a python built-in decorator (lets you do stuff before or after a function) that turns an instance method into a class method
just stick to the basic rule for now: every single function/method in your class should have "self" as first parameter.
^ does he even have a code editor with a good enough type checker?
yep now that works
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
im using idle
that won't work. the humanstat inside of the function is a different variable from the humanstat outside of it
oh wait did i make 2
!e
myvar = 123
def change_myvar():
myvar = 456
change_myvar()
print(myvar)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
123
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
^ no global, return or use self
!e
myvar = 123
def change_myvar():
myvar = 456
return myvar
myvar = change_myvar() # this becomes whatever is returned
print(myvar)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
456
terrible idea. get vscode or a jetbrains editor like sani
i mean its fun to try lol
thats jus what our school computers usin
i've only ever used IDLE for unit tests when i forgot how python works. there's not really much of a reason outside of that to use it
alt+m
what the fuck is a source based module
AttributeError: 'HumanPlayer' object has no attribute 'options'
happens when its the players turn
what is options?
a method or an attribute?
also resend code
!paste
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.
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.
you got it to work how you expected it to?
i mean theres still this
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
if you're still stuck then ping me and I'll see what i can do with the code you have so far.
@hard haven how would i put what sani said into practice in terms of my code
sry a bit slow lol
no worries, let me go read it quickly
what does your code look like at the moment?
@tardy trout
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
thats what i was thinking but whys that bad practice like sani said
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
damn that sounds long
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?
when the player wins they can choose their card again
i mean
the category/stat
goes for both human and computer
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
@tardy trout https://paste.pythondiscord.com/CWDQ
This is what I have done so far, but haven't had enough time to fix the other errors occurring.
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__
ill research that
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
β€οΈ
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.