一、分支結構-if等 練習題:
練習1:英制單位與公制單位互換
練習2:擲骰子決定做什么
練習3:百分制成績轉等級制
練習4:輸入三條邊長如果能構成三角形就計算周長和面積
練習5:個人所得稅計算器
練習6:用戶身份驗證
二、循環結構-while等 練習題:
練習1:輸入一個數判斷是不是素數。
練習2:輸入兩個正整數,計算最大公約數和最小公倍數。
練習3:打印三角形圖案。
練習4:實現1~100求和
練習5:實現1~100之間的偶數求和
練習6:輸入非負整數n計算n!
三、分支結構答案:
練習1:
""" 英制單位英寸和公制單位厘米互換 """ value = float(input('請輸入長度: ')) unit = input('請輸入單位: ') if unit == 'in' or unit == '英寸': print('%f英寸 = %f厘米' % (value, value * 2.54)) elif unit == 'cm' or unit == '厘米': print('%f厘米 = %f英寸' % (value, value / 2.54)) else: print('請輸入有效的單位')
練習2:
""" 擲骰子決定做什么事情 """ from random import randint face = randint(1, 6) if face == 1: result = '唱首歌' elif face == 2: result = '跳個舞' elif face == 3: result = '學狗叫' elif face == 4: result = '做俯卧撐' elif face == 5: result = '念繞口令' else: result = '講冷笑話' print(result)
說明: 上面的代碼中使用了random模塊的randint函數生成指定范圍的隨機數來模擬擲骰子。
練習3:
""" 百分制成績轉等級制成績 90分以上 --> A 80分~89分 --> B 70分~79分 --> C 60分~69分 --> D 60分以下 --> E """ score = float(input('請輸入成績: ')) if score >= 90: grade = 'A' elif score >= 80: grade = 'B' elif score >= 70: grade = 'C' elif score >= 60: grade = 'D' else: grade = 'E' print('對應的等級是:', grade)
練習4:
""" 判斷輸入的邊長能否構成三角形 如果能則計算出三角形的周長和面積 """ import math a = float(input('a = ')) b = float(input('b = ')) c = float(input('c = ')) if a + b > c and a + c > b and b + c > a: print('周長: %f' % (a + b + c)) p = (a + b + c) / 2 area = math.sqrt(p * (p - a) * (p - b) * (p - c)) print('面積: %f' % (area)) else: print('不能構成三角形')
說明: 上面的代碼中使用了math
模塊的sqrt
函數來計算平方根。用邊長計算三角形面積的公式叫做海倫公式。
練習5:
""" 輸入月收入和五險一金計算個人所得稅 """ salary = float(input('本月收入: ')) insurance = float(input('五險一金: ')) diff = salary - insurance - 3500 if diff <= 0: rate = 0 deduction = 0 elif diff < 1500: rate = 0.03 deduction = 0 elif diff < 4500: rate = 0.1 deduction = 105 elif diff < 9000: rate = 0.2 deduction = 555 elif diff < 35000: rate = 0.25 deduction = 1005 elif diff < 55000: rate = 0.3 deduction = 2755 elif diff < 80000: rate = 0.35 deduction = 5505 else: rate = 0.45 deduction = 13505 tax = abs(diff * rate - deduction) print('個人所得稅: ¥%.2f元' % tax) print('實際到手收入: ¥%.2f元' % (diff + 3500 - tax))
說明: 上面的代碼中使用了Python內置的abs()
函數取絕對值來處理-0
的問題。
練習6:
""" 用戶身份驗證 """ # import getpass # from getpass import getpass # from getpass import * username = input('請輸入用戶名: ') password = input('請輸入口令: ') # 輸入口令的時候終端中沒有回顯 # password = getpass.getpass('請輸入口令: ') if username == 'admin' and password == '123456': print('身份驗證成功!') else: print('身份驗證失敗!')
四、循環答案
練習1:
""" 輸入一個正整數判斷它是不是素數 """ from math import sqrt num = int(input('請輸入一個正整數: ')) end = int(sqrt(num)) is_prime = True for x in range(2, end + 1): if num % x == 0: is_prime = False break if is_prime and num != 1: print('%d是素數' % num) else: print('%d不是素數' % num)
練習2:
""" 輸入兩個正整數計算最大公約數和最小公倍數 """ x = int(input('x = ')) y = int(input('y = ')) if x > y: (x, y) = (y, x) for factor in range(x, 0, -1): if x % factor == 0 and y % factor == 0: print('%d和%d的最大公約數是%d' % (x, y, factor)) print('%d和%d的最小公倍數是%d' % (x, y, x * y // factor)) break
練習3:
""" 打印各種三角形圖案 * ** *** **** ***** * ** *** **** ***** * *** ***** ******* ********* """ row = int(input('請輸入行數: ')) for i in range(row): for _ in range(i + 1): print('*', end='') print() for i in range(row): for j in range(row): if j < row - i - 1: print(' ', end='') else: print('*', end='') print() for i in range(row): for _ in range(row - i - 1): print(' ', end='') for _ in range(2 * i + 1): print('*', end='') print()
練習4:
""" 用for循環實現1~100求和 """ sum = 0 for x in range(1, 101): sum += x print(sum)
""" 用while循環實現1~100求和 """ sum = 0 num = 1 while num <= 100: sum += num num += 1 print(sum)
練習5:
""" 用for循環實現1~100之間的偶數求和 """ sum = 0 for x in range(2, 101, 2): sum += x print(sum)
""" 用while循環實現1~100之間的偶數求和 """ sum, num = 0, 2 while num <= 100: sum += num num += 2 print(sum)
練習6:
""" 輸入非負整數n計算n! """ n = int(input('n = ')) result = 1 for x in range(1, n + 1): result *= x print('%d! = %d' % (n, result))