#๐Ÿ”’ Need help in restructuring my code to OOP

74 messages ยท Page 1 of 1 (latest)

wet venture
#

https://paste.pythondiscord.com/6W2A

This is my blackjack code, i want to implement OOP in it since I'm learning that and remake it, can anyone suggest me how can I do that since I'm struggling to understand where should I implement the class.

proven caveBOT
#

@wet venture

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.

bitter relic
wet venture
bitter relic
#

do you know how to define a class that has those two attributes?

brave sentinel
#

if you don't know, I can help write it

wet venture
brave sentinel
#

ok

#

yeah

#

but init not init

#

ok

wet venture
#

Yeah

bitter relic
brave sentinel
#

fixed

wet venture
#

Ok

brave sentinel
#

we will need a suit and rank property

#

that should be public

bitter relic
#

(don't worry about what "public" means for now. or "private".)

brave sentinel
#

fair

#

just to ensure no complexity i sent that

#

if you don't how how to do code blocks, just type py

bitter relic
#

!code

proven caveBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

bitter relic
#

it's like this ^

strange meteor
brave sentinel
#

i know

#

just to not add a leading _

#

don't do that here

bitter relic
#

let's drop this discussion, so we don't confuse OP

brave sentinel
#

i know

wet venture
#
class Cards():
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank```
Like this right?
brave sentinel
#

yes

#

but no parentheses needed

wet venture
#

I'm in mobile rn

#

So my bad if the formats a little off

wet venture
bitter relic
#

you'll create multiple instances of the Card class, though.

bitter relic
#

It looks to me like the only method you need is value, to do the work of calculating and returning the card's value.

#

Can you try writing that method?

wet venture
#

How can I do that

#

I am not able to think about that

bitter relic
#

Sure you are.

#

Have you written a function before?

brave sentinel
#

Here is an example:

class Foo:
    def __init__(self):
       self.ex = 5
    def do_something(self):
       pass

foo = Foo()
foo.do_something()
wet venture
brave sentinel
wet venture
#

I'm not able to figure out what will it calculate

brave sentinel
#

that is a logic thing, not a syntax thing we can help much on

brave sentinel
#

i don't know how cards work anyway

wet venture
#

I still don't get that how will we even use class to store the cards without making a list of tuples

wooden depot
brave sentinel
#

bruh i know what cards are

#

don't make this devolve

brave sentinel
#

so a simple list would do

wooden depot
#

List of tuples is fine, what wrong with it?

brave sentinel
#

looking at the original code, it could cleanly become

cards = [Card("A", "Spades"), Card(1, "Spades"), ...]
wet venture
#

Let me try to write the code of this

brave sentinel
#

@sleek skiff

sleek skiff
#

hey

#

yea

#

what is the question to this

#

nvm im fine

brave sentinel
#

scroll up

sleek skiff
#

k

proven caveBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.