Skip to content

Commit ff3092b

Browse files
committed
try
1 parent ff3265f commit ff3092b

9 files changed

Lines changed: 194 additions & 0 deletions

File tree

newcodes/account.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class Account(object):
5+
def __init__(self, number):
6+
self.number = number
7+
self.balance = 0
8+
9+
def deposit(self, amount):
10+
try:
11+
assert amount > 0
12+
self.balance += amount
13+
except:
14+
print("The money should be bigger than zero.")
15+
16+
def withdraw(self, amount):
17+
assert amount > 0
18+
if amount <= self.balance:
19+
self.balance -= amount
20+
else:
21+
print("balance is not enough.")
22+
23+
if __name__ == "__main__":
24+
a = Account(1000)
25+
a.deposit(-10)

newcodes/calculator.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class Calculator(object):
5+
is_raise = False
6+
def calc(self, express):
7+
try:
8+
return eval(express)
9+
except ZeroDivisionError:
10+
if self.is_raise:
11+
return "zero can not be division."
12+
else:
13+
raise
14+
15+
if __name__ == "__main__":
16+
c = Calculator()
17+
c.is_raise = True
18+
print(c.calc("8/0"))

newcodes/fibs_iter.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class Fibs:
5+
def __init__(self, max):
6+
self.max = max
7+
self.a = 0
8+
self.b = 1
9+
10+
def __iter__(self):
11+
return self
12+
13+
def __next__(self):
14+
fib = self.a
15+
if fib > self.max:
16+
raise StopIteration
17+
self.a, self.b = self.b, self.a + self.b
18+
return fib
19+
20+
if __name__ == "__main__":
21+
fibs = Fibs(5)
22+
print(list(fibs))

newcodes/fibs_yield.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
def fibs(max):
5+
"""
6+
斐波那契数列的生成器
7+
"""
8+
n, a, b = 0, 0, 1
9+
while n < max:
10+
yield b
11+
a, b = b, a + b
12+
n = n + 1
13+
14+
if __name__ == "__main__":
15+
f = fibs(10)
16+
for i in f:
17+
print(i, end=',')

newcodes/myrange.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class MyRange:
5+
def __init__(self, n):
6+
self.i = 1
7+
self.n = n
8+
9+
def __iter__(self):
10+
return self
11+
12+
def __next__(self):
13+
if self.i <= self.n:
14+
i = self.i
15+
self.i += 1
16+
return i
17+
else:
18+
raise StopIteration()
19+
20+
if __name__ == "__main__":
21+
x = MyRange(7)
22+
print([i for i in x])

newcodes/normal_rectangle.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
"""
5+
study __getattr__ and __setattr__
6+
"""
7+
8+
class Rectangle:
9+
"""
10+
the width and length of Rectangle
11+
"""
12+
def __init__(self):
13+
self.width = 0
14+
self.length = 0
15+
16+
def setSize(self, size):
17+
self.width, self.length = size
18+
def getSize(self):
19+
return self.width, self.length
20+
21+
if __name__ == "__main__":
22+
r = Rectangle()
23+
r.width = 3
24+
r.length = 4
25+
print(r.getSize())
26+
r.setSize( (30, 40) )
27+
print(r.width)
28+
print(r.length)

newcodes/special_rectangle.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
class NewRectangle:
5+
def __init__(self):
6+
self.width = 0
7+
self.length = 0
8+
9+
def __setattr__(self, name, value):
10+
if name == "size":
11+
self.width, self.length = value
12+
else:
13+
self.__dict__[name] = value
14+
15+
def __getattr__(self, name):
16+
if name == "size":
17+
return self.width, self.length
18+
else:
19+
raise AttributeError
20+
21+
if __name__ == "__main__":
22+
r = NewRectangle()
23+
r.width = 3
24+
r.length = 4
25+
print(r.size)
26+
r.size = 30, 40
27+
print(r.width)
28+
print(r.length)

newcodes/try_else.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
while 1:
4+
try:
5+
x = input("the first number:")
6+
y = input("the second number:")
7+
8+
r = float(x)/float(y)
9+
print(r)
10+
except Exception as e:
11+
print(e)
12+
print("try again.")
13+
else:
14+
break

newcodes/try_except.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
4+
while 1:
5+
print("this is a division program.")
6+
c = input("input 'c' continue, otherwise logout:")
7+
if c == 'c':
8+
a = input("first number:")
9+
b = input("second number:")
10+
try:
11+
print(float(a)/float(b))
12+
print("*************************")
13+
except (ZeroDivisionError, ValueError) as e:
14+
print(e)
15+
print("*************************")
16+
#except ValueError:
17+
# print("please input number.")
18+
# print("************************")
19+
else:
20+
break

0 commit comments

Comments
 (0)