Python案例分享


1.過橋(爬金字塔):

  1 i = 1
  2 while i <= 9: 3     if i < 6: 4         j = 0 5         while j < i: 6             print('*',end=' ') 7             j += 1
  8     elif i >= 6: 9         j = 5
 10         while j > i - 5: 11             print('*',end=' ') 12             j -= 1
 13     print('') 14     i += 1
 15 

 

 

2.動態打印9*9乘法表多次:

  1 def print_99_once(): 2     i = 1
  3     while i <= 9: 4         j = 1
  5         while j <= i: 6             print('%d*%d=%d' %(j,i,i*j),end=' ') 7             j += 1
  8         print('') 9         i += 1
 10 
 11 def print_99_more(num): 12     i = 1
 13     while i <= num: 14 print_99_once() 15         i += 1
 16 
 17 
 18 num = int(input('打印99乘法表多次,請輸入一個數打印幾次:')) 19 print_99_more(num)

 

3.動態計算1~?的累計和:

  1 def sum_1_n(a): 2     num = 1
  3     result = 0 4     while num <= a: 5         result = result + num 6         num += 1
  7     return result 8 
  9 a = int(input('計算1-?的累計和,請輸入?的值:')) 10 total = sum_1_n(a) 11 print(total)

 

4.隨機給老師分配辦公室(辦公室至少有1人):

  1 import random 2 offices = [[],[],[]]#總共3個辦公室
  3 teachers = ['A','B','C','D','E','F','G','H']#老師名單
  4 offices[0].append(teachers[random.randint(0,7)])#保證每個辦公室至少有1個老師
  5 teachers.remove(offices[0][0]) 6 offices[1].append(teachers[random.randint(0,6)]) 7 teachers.remove(offices[1][0]) 8 offices[2].append(teachers[random.randint(0,5)]) 9 teachers.remove(offices[2][0]) 10 for teacher in teachers: 11     office = offices[random.randint(0,2)] #隨機索引出一個辦公室
 12 office.append(teacher) 13 i = 1 #表示第幾個辦公室,默認從1開始
 14 for office in offices: #13~16可以用enumerate替代
 15     print('辦公室%d有:%d人' %(i,len(office))) 16     i += 1
 17     for teacher in office: 18         print('%s' %teacher,end=' ') 19     print('='*30)

 

 

5.統計Python哲學這段文字中'' to ''出現的次數

content = """Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!""" count = content.count("to") print("to在上面的文字共出現:%d次" % count)

這段英文有幾大特點:不在一行    靠左對齊   都已句號結尾    出現多次單引號

因此,轉字符串count時bug不斷。【把一段英文轉為字符串不只有一對單引號,一對雙引號,還有一對3個單引號

 

6.求1-100累計和:

  1 num = 1
  2 sum = 0 3 while num <= 100: 4     sum += num 5     num += 1
  6 print ('1~100包含(1和100)的和是:%d'%sum)

 

7.求1-100中偶數累計和:

  1 num = 1
  2 sum = 0 3 while num <= 100: 4     if num%2 == 0: 5         sum += num 6     num += 1
  7 print('1-100(包含1和100)中偶數和是:%s'%sum)

 

8.打印1-100:

  1 num = 1
  2 while num <= 100: 3     print(num) 4     num += 1

 

9.打印三角形:

  1 i = 1
  2 while i <= 5: 3     j = 0 4     while j < i: 5         print('*',end='')#end=''阻止打印默認行為
  6         j += 1
  7     i += 1
  8     print('')#換行等價打印\n

 

10.打印矩形:

  1 i = 0 2 while i < 5: 3     print('*'*5) 4     i += 1

 

11.猜拳游戲 (按q退出):

  1 while True: 2     import random 3     player = input('請出拳:0石頭,1剪刀,2布,或者按q退出') 4     computer = random.randint(0,2) 5     if player == 'q': 6         print('='*10) 7         break
  8     else: 9         player =int(player) 10         if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0): 11             print('玩家贏啦') 12         elif player == computer: 13               print('平局') 14         else: 15             print('你輸了')

 

12.用戶輸入分數獲取等級(循環 + 按q退出):

  1 while True: 2     score = input('請輸入你的考試成績或者按q退出:') 3     if score == 'q': 4         break
  5     else: 6         score = int(score) 7         if 90<= score <100: 8             print('你的考試等級A') 9         elif 80<= score <90: 10             print('你的考試等級B') 11         elif 70<= score <80: 12             print('你的考試等級C') 13         elif 60<= score <70: 14             print('你的考試等級D') 15         else: 16             print('你的考試不及格')

 

13.字典名片管理系統:

print('='*35) print('\t名片管理系統1.1') print('\t1.新增一個名片') print('\t2.刪除一個名片') print('\t3.修改一個名片') print('\t4.查看一個名片') print('\t5.顯示名片列表') print('\t6.退出名片管理系統') print('='*35) card_infos = [] while True: num = int(input('請輸入您選擇的編號:')) if num == 1: name = input('請輸入要增加名片的姓名:') qq = int(input('請輸入要增加名片的qq:')) wechat = input('請輸入要增加名片的微信:') card_info = {} card_info['姓名'] = name card_info['QQ'] = qq card_info['微信'] = wechat card_infos.append(card_info) print('名片列表如下:') print('姓名\tQQ\t微信') for card_info in card_infos: print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信'])) elif num == 2: flag = 0 select = int(input('輸入1根據內容刪除;輸入2根據下標刪除:')) if select == 1: name = input('請輸入要刪除名片的姓名:') for card_info in card_infos: if card_info.get('姓名') == name: card_infos.remove(card_info) print('刪除成功!') print('刪除后名片列表如下:') print('姓名\tQQ\t微信') for card_info in card_infos: print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信'])) flag = 1
                    break
                elif flag == 0: print('您要刪除的名片不存在!') elif select == 2: index = int(input('請輸入要刪除名片序號:')) if index <= len(card_infos)+1: del card_infos[index-1] name =  card_infos[index-1]['姓名'] print('刪除成功!') print('刪除后名片列表如下:') print('姓名\tQQ\t微信') for card_info in card_infos: print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信'])) else: print('您要刪除的名片不存在!') else: print('輸入有誤!輸入1根據內容刪除;輸入2根據下標刪除') continue
    elif num == 3: name = input('請輸入要修改名片的姓名:') content = input('請輸入要修改的內容:') new_content = input('請輸入新內容:') for card_info in card_infos: if card_info.get('姓名') == name: if content == card_info.get('姓名'): card_info['姓名'] = new_content print('修改成功!') print('刪除后名片列表如下:') print('姓名\tQQ\t微信') for card_info in card_infos: print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信'])) flag = 1
                    break
                elif content == str(card_info.get('QQ')): new_content = int(new_content) card_info['QQ'] = new_content print('修改成功!') print('刪除后名片列表如下:') print('姓名\tQQ\t微信') for card_info in card_infos: print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信'])) flag = 1
                    break
                elif content == card_info.get('微信'): card_info['微信'] = new_content print('修改成功!') flag = 1
                    print('刪除后名片列表如下:') print('姓名\tQQ\t微信') for card_info in card_infos: print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信'])) break
                elif flag == 0: print('您要修改的名片不存在!') else: print('您要修改的名片不存在!') elif num == 4: name = input('請輸入要查看名片的姓名:') for card_info in card_infos: if card_info.get('姓名') == name: print('姓名\tQQ\t微信') print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信'])) flag = 1
                break
            elif flag == 0: print('您要查看的名片不存在!') elif num == 5: print('名片列表如下:') print('姓名\tQQ\t微信') for card_info in card_infos: print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信'])) elif num == 6: break
    else: print('輸入有誤!請輸入正確的編號')

 

14.你輸什么我都給你倒過來:

  1 while True: 2     content = input('你輸入什么我都能給你倒過來輸出:') 3     if content == 'q': 4         break
  5     else: 6         if type(content) == int: 7             content = int(content) 8             print(content[::-1]) 9         else: 10             print(content[::-1])

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM