赞
踩
1.过桥(爬金字塔):
1 i = 1
2 while i <= 9:3 if i < 6:4 j =05 while j <6 print j>
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 defprint_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 defprint_99_more(num):12 i = 1
13 while i <=num:14print_99_once()15 i += 1
16
17
18 num = int(input('打印99乘法表多次,请输入一个数打印几次:'))19 print_99_more(num)
3.动态计算1~?的累计和:
1 defsum_1_n(a):2 num = 1
3 result =04 while num <=a:5 result = result +num6 num += 1
7 returnresult8
9 a = int(input('计算1-?的累计和,请输入?的值:'))10 total =sum_1_n(a)11 print(total)
4.随机给老师分配办公室(办公室至少有1人):
1 importrandom2 offices = [[],[],[]]#总共3个办公室
3 teachers = ['A','B','C','D','E','F','G','H']#老师名单
4 offices[0].append(teachers[random.randint(0,7)])#保证每个办公室至少有1个老师
5teachers.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 inteachers:11 office = offices[random.randint(0,2)] #随机索引出一个办公室
12office.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 inoffice: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 =03 while num <= 100:4 sum +=num5 num += 1
6 print ('1~100包含(1和100)的和是:%d'%sum)
7.求1-100中偶数累计和:
1 num = 1
2 sum =03 while num <= 100:4 if num%2 ==0:5 sum +=num6 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 =04 while j <5 print>
6 j += 1
7 i += 1
8 print('')#换行等价打印\n
10.打印矩形:
1 i =02 while i < 5:3 print('*'*5)4 i += 1
11.猜拳游戏 (按q退出):
1 whileTrue:2 importrandom3 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 whileTrue: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=[]whileTrue:
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 incard_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 incard_infos:if card_info.get('姓名') ==name:
card_infos.remove(card_info)print('删除成功!')print('删除后名片列表如下:')print('姓名\tQQ\t微信')for card_info incard_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 incard_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 incard_infos:if card_info.get('姓名') ==name:if content == card_info.get('姓名'):
card_info['姓名'] =new_contentprint('修改成功!')print('删除后名片列表如下:')print('姓名\tQQ\t微信')for card_info incard_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_contentprint('修改成功!')print('删除后名片列表如下:')print('姓名\tQQ\t微信')for card_info incard_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_contentprint('修改成功!')
flag= 1
print('删除后名片列表如下:')print('姓名\tQQ\t微信')for card_info incard_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 incard_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 incard_infos:print('%s\t%d\t%s' %(card_info['姓名'],card_info['QQ'],card_info['微信']))elif num == 6:break
else:print('输入有误!请输入正确的编号')
14.你输什么我都给你倒过来:
1 whileTrue: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])
5>6>Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。