d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Some Help With Python > Please :)
Prev12345Next
Add Reply New Topic New Poll
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 11:09am
how about class RightTriangle(object):
whats that? you have that as the first statement of the function and everything is inside, yet i have no idea what it does xD
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 11:17am
Quote (known954 @ Feb 10 2015 11:09am)
how about class RightTriangle(object):
whats that? you have that as the first statement of the function and everything is inside, yet i have no idea what it does xD


It is probably your first exposure to object oriented programming, basically created an object with specific functions inside of it, at the bottom where I run it under if __name__ == "__main__" though is where you see it implemented. You can read the function names inside of the class and see what they're generally doing.
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 11:21am
yeah, its my first programming class ever and a lot of the functions you use I am not familiar with, such as : def ; class ; self ; check


edit: but thats a good thing! i will experiment with these things

This post was edited by known954 on Feb 10 2015 11:23am
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 11:37am
Quote (known954 @ Feb 10 2015 11:21am)
yeah, its my first programming class ever and a lot of the functions you use I am not familiar with, such as : def ; class ; self ; check


def creates a function, which can be standalone or created inside of a class.

Code

def name():
""" This asks your name and returns a reference of your name"""
name = input("What's your name? ")
return name




Code

class Name(object):

def __init__(self, name):
self.name = name

def uppercase(self):
""" Make your name uppercase """
return self.name.upper()

def titled(self):
""" Make your name titled """
return self.name.title()

def lowercase(self):
""" Make your name lowercase """
return self.name.lower()


So creating them is half the battle, now you have to use them properly.

A function you can use a few ways, you call call it directly name() or you can assign it to something to reference it later my_name = name()

self can be anything, you don't have to call it self but it is a Python standard to use the value self, which is passed around a class and inherited by other functions and objects within it. The advantage of using OOP is this inheritance.

So back to using this mess.

If you save those in the same file and call it like this

python3 -i scriptname.py

You are able to play around with the functions within it

Code
>>> my_name = name()
>>> change = Name(my_name)
>>> dir(change)
>>> change.uppercase
<bound method Name.uppercase of <__main__.Name object at 0x7fa0550ad898>>
>>> change.uppercase()
'RICK'
>>> change.lowercase()
'rick'
>>> change.titled()
'Rick'
>>>
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 12:17pm
what part of the function is making it ask for another input 1 after receiving 3 inputs/an invalid input? it loops indefinitely?
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 12:18pm
Quote (known954 @ Feb 10 2015 12:17pm)
what part of the function is making it ask for another input 1 after receiving 3 inputs/an invalid input? it loops indefinitely?


In my original function I put it under a while loop, so it will continually ask for sides, :P

Code
if __name__ == "__main__":
while 1:
try:
rt = RightTriangle()
rt.ask()
print('Before:')
print(rt.d)
rt.square()
print('After:')
print(rt.d)
print(rt.check())
except KeyboardInterrupt:
pass


It basically says: while 1 == True or while 1 is True, the True is implied.

while 1: which always evaluates to True, 0 will be False, so will "" and None.

This post was edited by j0ltk0la on Feb 10 2015 12:20pm
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 12:31pm
Ok that makes sense. how about this....

Code
Length of side 1:
Invalid input
[0, 0, 0]
These three values can create a right triangle.
Length of side 1: 3
Length of side 2: 4
Length of side 3: 5
[9.0, 16.0, 25.0]
These three values can create a right triangle.
Length of side 1: 3
Length of side 2: 4
Length of side 3: 6
[9.0, 16.0, 36.0]
These three values cannot create a right triangle.
Length of side 1: 3
Length of side 2: 4
Length of side 3: 0
[0.0, 9.0, 16.0]
These three values cannot create a right triangle.
Length of side 1: 0
Length of side 2: 0
Length of side 3: 0
[0.0, 0.0, 0.0]
These three values can create a right triangle.


why is it saying it can when i put in a invalid entry or 0/0/0... but not when i put 0 as 1 of the 3 inputs?
it should say cannot
Member
Posts: 62,215
Joined: Jun 3 2007
Gold: 9,039.20
Feb 10 2015 01:16pm
Quote (known954 @ Feb 10 2015 12:31pm)
Ok that makes sense. how about this....

Code
Length of side 1:
Invalid input
[0, 0, 0]
These three values can create a right triangle.
Length of side 1: 3
Length of side 2: 4
Length of side 3: 5
[9.0, 16.0, 25.0]
These three values can create a right triangle.
Length of side 1: 3
Length of side 2: 4
Length of side 3: 6
[9.0, 16.0, 36.0]
These three values cannot create a right triangle.
Length of side 1: 3
Length of side 2: 4
Length of side 3: 0
[0.0, 9.0, 16.0]
These three values cannot create a right triangle.
Length of side 1: 0
Length of side 2: 0
Length of side 3: 0
[0.0, 0.0, 0.0]
These three values can create a right triangle.


why is it saying it can when i put in a invalid entry or 0/0/0... but not when i put 0 as 1 of the 3 inputs?
it should say cannot


Technically, 0 = the sum of 0 + 0, so something will have to be done about that.

A condtional to check if 0 or 0.0 in self.d

This post was edited by j0ltk0la on Feb 10 2015 01:21pm
Member
Posts: 1,097
Joined: Apr 16 2010
Gold: 0.00
Feb 10 2015 03:41pm
Well... i guess this is when I'm supposed to ask what a conditional is, and how to apply one.
Member
Posts: 13,578
Joined: Jul 27 2010
Gold: 2,285.00
Feb 10 2015 05:24pm
Quote (known954 @ Feb 10 2015 02:31pm)
why is it saying it can when i put in a invalid entry or 0/0/0... but not when i put 0 as 1 of the 3 inputs?
it should say cannot

Now you're thinking like a software tester.
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll