(1)税
写法1:
#税 s = float(input()) if s <= 2000: print("Isento") elif 3000 >= s >= 2000.01: X = (s - 2000.01)*0.08 print("R$ %.2f"%X) elif 4500.00 >= s >= 3000.01: X = (s - 3000.01)*0.18 + (1000*0.08) print("R$ %.2f"%X) else: X = (s - 4500.00)*0.28 + (1000*0.08) + (1500*0.18) print("R$ %.2f"%X)
写法2:
x = float(input()) tot = 0 if x > 4500: tot += (x - 4500) * 0.28 x = 4500 if x > 3000: tot += (x - 3000) * 0.18 x = 3000 if x > 2000: tot += (x - 2000) * 0.08 x = 2000 if tot == 0: print("Isento") else: print("R$ %.2f" % tot)
(2) 游戏时间2
#游戏时间2 a,b,c,d = map(int,input().split()) start = a * 60 + b end = c * 60 + d # 当结束时间和开始时间位于同一天的时候 if end > start : x = (end - start) / 60 y = (end - start) % 60 # 当结束时间和开始时间不是同一天的时候 if end <= start : x = (24 * 60 - start + end) / 60 y = (24 * 60 - start + end) % 60 print("O JOGO DUROU %d HORA(S) E %d MINUTO(S)"%(x,y))
(3)求解一元二次方程的根
#求解一元二次方程的根 a,b,c =map(float,input().split()) if b ** 2 - 4 * a * c < 0 or a == 0 : print("Impossivel calcular") else: R1 = (-b + (b ** 2 - 4 * a * c)**0.5 ) / (2 * a) R2 = (-b - (b ** 2 - 4 * a * c)**0.5 ) / (2 * a) print("R1 = %.5f"%R1) print("R2 = %.5f"%R2)
(4)求列表中所有元素之和
解法一:循环语句
#求列表中所有元素之和 def sum_list(lst): total = 0 for num in lst: total += num return total my_list = [1, 2, 3, 4, 5, 7.5] print(sum_list(my_list)) # 输出22.5
函数知识点👉🔗:http://t.csdnimg.cn/Xs52R
解法二:递归
#递归 def list_sum(num_list): if len(num_list) == 1: return num_list[0] else: return num_list[0] + list_sum(num_list[1:]) print(list_sum([1, 2, 3, 4, 5, 7.5]))