-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass.py
164 lines (114 loc) · 3.5 KB
/
class.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#CLASSES
#OBJECTS
#instance variable
#methods
#raise for exceptions
#__str__
class Student:
def __init__(self,name,house):
if not name: # or if name==""
raise ValueError("Missing Name")
if house not in ["bng","cha","aaa"]:
raise ValueError("Incorrect Place/house")
self.name = name
self.house = house
def __str__(self):
return f"{self.name} is from {self.house}"
def main():
student=get_student()
print(f"{student.name} is from {student.house}")
def get_student():
name=input("Name: ")
house=input("House: ")
student=Student(name,house)
return student
if __name__=="__main__":
main()
class Student:
def __init__(self,name,house,patronus):
if not name:
raise ValueError("Missing Name")
if house not in ["bng","cha","aaa"]:
raise ValueError("Incorrect Place/house")
self.name = name
self.house = house
self.patronus = patronus
def __str__(self):
return f"{self.name} is from {self.house}"
def charm(self):
if self.patronus=="stag":
return "cha"
elif self.patronus=="ccc":
return "ccc"
else:
return "everythins is working fine"
def main():
student=get_student()
print(f"{student.name} is from {student.house}")
print("Horreyyy")
print(student.charm())
def get_student():
name=input("Name: ")
house=input("House: ")
patronus=input("patronus: ")
student=Student(name,house,patronus)
return student
if __name__=="__main__":
main()
class Student:
def __init__(self,name,house):
if not name:
raise ValueError("Missing Name")
if house not in ["bng","cha","aaa"]:
raise ValueError("Incorrect Place/house")
self.name = name
self.house = house
def __str__(self):
return f"{self.name} is from {self.house}"
def main():
student=get_student()
print(student)
#print(f"{student.name} is from {student.house}") #both will work fine above and below
def get_student():
name=input("Name: ")
house=input("House: ")
student=Student(name,house)
return student
if __name__=="__main__":
main()
#class method below is the one way
import random
class Hat:
def __init__(self):
self.houses=['bangalore', 'Karnataka','Channasandra','Chandapura']
def sort(self, name):
print(name, "is in ",random.choice(self.houses))
hat = Hat()
hat.sort('Harry')
#class method using the creation of the variables
import random
class Hat:
houses =['bangalore', 'Karnataka','Channasandra','Chandapura'] #directly create a variable houses and use within the class
@classmethod
def sort(cls,name):
print(name, "is in ", random.choice(cls.houses))
#instead a creating one more variable use directly Hat
Hat.sort("Chandu")
class vault:
def __init__(self,party=0,party2=0,party3=0):
self.party=party
self.party2=party2
self.party3=party3
def __str__(self):
return f'{self.party} "gowda", {self.party2} "chandu", {self.party3} "cha'
print(vault()) #if we dont give the values it taked default as o as defined above in the classs
print(vault(10,20,30)) #if we give the values then it will take the values that we defined
#constants
class Cat:
MEOW=3
def meow(self):
for _ in range(Cat.MEOW):
print("Yup")
#cat=Cat
#cat.meow() or
Cat().meow()