本帖最后由 ooxx7788 于 2017-11-23 15:00 编辑
看在台湾同胞的份上帮你写个。# 4
def foo4_1(a, b):
if a > b:
return a * b
else:
return a + b
print('#4')
print(foo4_1(3, 4))
print(foo4_1(4, 3))
def foo4_2(a, b):
if b == 0:
return -1
else:
return a / b
print(foo4_2(4, 2))
print(foo4_2(4, 0))
print()
# 5
def get_max(a, b, c):
return max(a, b, c)
print('#5')
print(get_max(3, 6, 8))
def get_sum(a, b, c, d):
return sum((a, b, c, d))
print(get_sum(5, 6, 7, 8))
def get_average(a, b, c, d):
return get_sum(a, b, c, d) / 4
print(get_average(5, 6, 7, 8))
print()
# 6
def bill(hour):
if hour < 50:
return hour * 60 * 0.3
else:
return 50 * 60 * 0.3 + (hour - 50) * 0.2
print('#6')
print(bill(40))
print(bill(60))
print()
# 7
def rate_exchange(nt, rate):
return nt * rate
print('#7')
print(rate_exchange(10000, 0.2201))
print()
# 9
def fib(n):
f0 = 1
f1 = 1
result = [f0, f1]
while n - 2:
result.append(f0 + f1)
f0, f1 = f1, f0 + f1
n -= 1
return result
print('#9')
print(fib(10))
print()
# 10
def print_stars(n):
i = 1
while n >= i:
print(str.center('* ' * i, n + n - 1))
i += 1
print('#10')
print_stars(15)
结果:#4
7
12
2.0
-1
#5
8
26
6.5
#6
720.0
902.0
#7
2201.0
#9
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
#10
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * * * * *
* * * * * * * * * * * * * * *
最后一个好像论坛有问题
|