#πŸ”’ Question from dog class

204 messages Β· Page 1 of 1 (latest)

tough isle
#
class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def sit(self):#line 5
    # this space right here
        print(f"{self.name} is now sitting.")

my_dog = Dog('Willie', 6)
your_dog = Dog('Lucy', 3)

print(f"My dog's name is {my_dog.name}.")#line num 11
print(f"My dog is {my_dog.age} years old.")# line num 12
my_dog.sit()

print(f"\nYour dog's name is {your_dog.name}.")
print(f"Your dog is {your_dog.age} years old.")
your_dog.sit()

why does the author not try to write line num 11 and line num 12 under line 5

worldly solarBOT
#

@tough isle

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.

tough isle
#

@native dust

#

it was crowded in main discussion channel so I thought we could talk here

#

I just reposted your message

#

if you want

#

hey let me know if you're here

native dust
#

ok

tough isle
#

what was your question?

native dust
#

what is method i forgot

#

like which is called method

tough isle
#

it's just a function in the class

#

it's a function that has self

#

that you can call using an object of that class

#

so in the code above, __ init__ and sit are methods

#
class Cat:
  def meow(self, volume=50):
    ...

def foo(a, b):
  ...```
native dust
#

o

tough isle
#

which ones are methods there

#

I'm gonna give the answer as a spoiler, don't click it until you have a guess

#

||meow is the only method. it is in a class and it has self parameter. foo is just a plain function, it's not a method because it's outside any class.||

native dust
#

anything with function that is indented below class is method

tough isle
#
def thing():
  ...

if True:
    def bar(a, b):
        ...

class A:
  def xyz(self, volume=50):
    ...

def foo(a, b):
  ...```
#

which ones are methods @native dust

tough isle
#

yes!!

#

really good

native dust
#
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

my_new_car = Car('audi', 'a4', 2019)
print(my_new_car.get_descriptive_name())#line 11

why did the author write line 11 like that

they could have wrote the followings:

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        print(long_name.title())

my_new_car = Car('audi', 'a4', 2019)
my_new_car.get_descriptive_name()

#

@tough isle

tough isle
#

looking

#

why did they put that comment? is that what you're asking

#

the #line 11

native dust
#

why did they use print when they could have print inside the def

#

what is the use of return

tough isle
#

oh why did they print?

worthy crane
#

do u understand how return works or u asking that

tough isle
#

i see

#

in this case what you wrote does the same results as what they wrote

#

i think they just wanted the method to return the name since they called it get_descriptive_name

#

and they're printing it when they called it so you can see the result

#

i think he's understanding how return works s

#

also

#

oh nevermind

native dust
# tough isle i see
class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

my_new_car = Car('audi', 'a4', 2019)
print(my_new_car)# prints <__main__.Car object at 0x000001EB079D6180> smh


why does this not work properly

lets take a look at an example code:

def x(number):
    return number+4
y=x(5)
print(y)


you just printed y and it gave an output of 9

tough isle
#

here

#

so your bottom code works, right?

native dust
tough isle
#

so in the top code

#

what is it that you're printing?

native dust
#

in the bottom code i just wrote print(y) and it worked in teh tip code i wrote that it is saying random stuff

tough isle
#

what are you giving to print()

native dust
tough isle
#

exactly

native dust
#

why?

tough isle
#

that's why it prints something else

native dust
tough isle
#

you see that you are printing the car?

native dust
tough isle
#

you're printing my_new_car right?

native dust
tough isle
#

instead of my_new_car.get_descriptive_name()

#

you're printing the car itself

#

not the result from the get_descriptive_name() called on the car

#

I'll show you

native dust
#

k

tough isle
#

!e


class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

car = Car('audi', 'a4', 2019)
print("car =", car)
print("calling car.get_descriptive_name()")
name = car.get_descriptive_name()
print("name =", name)

car2 = Car('toyota', 'corolla', 2023)
print("car2 =", car2)
print("calling car2.get_descriptive_name()")
name2 = car2.get_descriptive_name()
print("name2 =", name2)```
worldly solarBOT
tough isle
#

do you see the difference between printing the car and printing the result of calling one of the methods on the car (cars name)

native dust
#

?

tough isle
#

printing car vs printing cars name

#

we can change how it shows

#

!e

class Car:
    def __init__(self, make, model, year):
        self.make = make
        self.model = model
        self.year = year
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()
    def __str__(self):
        return f"[==Car: {self.make} {self.model}==]"

car = Car('audi', 'a4', 2019)
print("car =", car)
name = car.get_descriptive_name()
print("name =", name)```
worldly solarBOT
tough isle
#

@native dust

#

now you see it isn't printing random stuff

#

when we do print(car)

#

it's printing out the car in a nice format

#

using the __str__ method we added

#

print calls it automatically if you print an object

native dust
#

and how do i know when to add those things and when to not

tough isle
#

you never have to

tough isle
native dust
#

and not the car names

tough isle
#

do you understand why something different shows when you do

print(car)

vs

print(car.get_descriptive_name())

#

?

tough isle
#

it's actually not a weird message

#

let's look at it

#

<__main__.Car object at 0x000001EB079D6180>

#

that's what you saw, right?

#

when you did print(my_new_car)

native dust
#

ye

tough isle
#

1 - my_new_car is an instance of Car class. it's an object. that's why it says "Car object"

2 - you're in the main script file. It's technical name is __main__ - it's not a library you imported so it's the "main* script.

So __main__ is where Car is defined, so the full name of the Car class is __main__.Car.

3 - The way the unique Car object is identified is by its memory address. This particular object is at the address 0x00001EB079S6180 - thus "car object at 0x00001EB079S6180 (its memory address)".

#

put it all together and you get

<__main__.Car object at 0x000001EB079D6180>

#

that's how a new class prints its objects by default if you don't make a __str__ method

tough isle
#

yeah

native dust
# tough isle yeah

what do i write instead of snip in the pg 163 in the bottom page, and 201 in the web browser

#

under the chapter setting a default value for an attribute

tough isle
#

lemme open the pdf

native dust
tough isle
#

here?

#

I assume you put the contents of get_descriptive_name from before

#

remember? you have that function in the code you pasted already

worthy crane
# tough isle

i been meaning to ask but, tha hell kind of terminal u have, this looks like msdos

tough isle
#
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()```
tough isle
#

i just had the colors reversed so it would be dark

#

I wish they would just use ... instead of snip

native dust
#

so what is the full code

tough isle
#

or just show the whole code

native dust
#

so what is the full code

worthy crane
native dust
native dust
worthy crane
#

ideally without copying over what was sent here

native dust
tough isle
#
class Car:
    def __init__(self, make, model, year)
        """Initialize attributes to describe a car."""
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0
    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()
    def read_odometer(self):
        """Print a statement showing the car's mileage."""
        print(f"This car has {self.odometer_reading} miles on it.")


my_new_car = Car('audi', 'a4', 2019)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()
tough isle
native dust
#
class Car:
    def __init__(self, make, model, year):# line 2
        self.make = make
        self.model = model
        self.year = year
        self.odometer_reading = 0#line 6

    def get_descriptive_name(self):
        long_name = f"{self.year} {self.make} {self.model}"
        return long_name.title()

    def read_odometer(self):
        print(f"This car has {self.odometer_reading} miles on it.")

my_new_car = Car('audi', 'a4', 2019)
print(my_new_car.get_descriptive_name())
my_new_car.read_odometer()

in line 6 how did they use self.odometer_reading even though odometer_reading wasnt even in the bracket of line 2

tough isle
#

all i did was take the example and paste in the method from before

#

and cleaned up the spaces

#

it's on line 6

native dust
#

yes

eternal elk
tough isle
#

if anyone wants to follow along in the pdf

worthy crane
#

what the

#

perms abuse

tough isle
#

anyone can post files

worthy crane
#

lmfao

tough isle
#

and it's available free online

#

you can't?

native dust
#

yea

#

bro u can

worthy crane
#

unless some1 got perms like u

native dust
#

o

#

ye

tough isle
#

oh

worthy crane
#

a pdf can be surprisingly harmful

tough isle
#

alright no more pdf

native dust
tough isle
#

write a rock paper scissors game

native dust
tough isle
#

or a game where you have to unscramble a word

worthy crane
native dust
#

i havent learn random modules

worthy crane
eternal elk
native dust
#

i dont have that file anymore

#

ill write it again

keen basin
tough isle
#

!e

l = [1, 2, 3, 4]
from random import shuffle
shuffle(l)
print(l)```
#

wtf

worldly solarBOT
worthy crane
#

did we just witness a useless shuffle

tough isle
#

!e

from random import choice, shuffle
words = ["greeting", "shuffle", "goodbye"]
word = choice(words)
print(word)
letters = list(word)
print(letters)
shuffle(letters)
print(letters)
print("".join(letters))```
worldly solarBOT
tough isle
#

@native dust a few new tools for you that you can use ^

#

for your games

native dust
# tough isle <@1160893833794568232> a few new tools for you that you can use ^

player_1=input("Player 1:Rock paper or scissor?")
player_2=input("Player 2:Rock paper or scissor?")
if player_1==player_2:
print("Tie!")
elif player_1=="rock" and player_2=="scissors" or player_1=="paper" and player_2=="rock" or player_1=="scissors" and player_2=="paper":
print("Player 1 Wins!")
else:
print("Player 2 Lost!")

worldly solarBOT
#

Hey @native dust!

Please edit your message to use a code block

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

This will result in the following:

print('Hello, world!')```
native dust
#
player_1=input("Player 1:Rock paper or scissor?")
player_2=input("Player 2:Rock paper or scissor?")
if player_1==player_2:
    print("Tie!")
elif player_1=="rock" and player_2=="scissors" or player_1=="paper" and player_2=="rock" or player_1=="scissors" and player_2=="paper":
    print("Player 1 Wins!")
else:
    print("Player 2 Lost!")
#

how do i rewrite this to class if i want to @tough isle

tough isle
#

think about what kind of class you want

#

btw you can use random.choice to have the other player be AI @native dust

worthy crane
#

i find it funny how those are still called ai nowadays

worthy crane
#

but ig even LLMs are called ai so watever

native dust
#

but can i make class with the code i have

eternal elk
#

it would be fun to make a rock-paper-scissors AI that tries to take advantage of flaws in our estimation of randomness, or tries to psyche out the opponent

tough isle
#

definitely

#

you have to come up with how you would like to structure it though

#

maybe other ppl here have some ideas

native dust
#

bruh i gtg

#

cya

keen basin
#

i have one idea

tough isle
#

cya

#

put it down and tag him

#

oh that might be a little advanced lol

keen basin
#

oh

#

i was actually trying to make a online cardgame

#

its fun. like managing messages

#

from clients and authorative server

tough isle
#

let's go back to #python-discussion

worldly solarBOT
#
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.