翼度科技»论坛 编程开发 python 查看内容

Python中21道个程序小练习

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
1.使用格式化输出的三种方式实现以下输出(name换成自己的名字,既得修改身高体重,不要厚颜无耻)
  1. name = 'ABDMLBM'
  2. height = 175
  3. weight = 140
  4. # "My name is 'Nick', my height is 180, my weight is 140"
  5. print('My name is %s,my height is %s,my weight is %s'%(name,height,weight))
  6. print('My name is {},my height is {},my weight is {}'.format(name,height,weight))
  7. print(f'My name is {name},my height is {height},my weight is {weight}')
复制代码
2.输入姑娘的年龄后,进行以下判断:


  • 如果姑娘小于18岁,打印“不接受未成年”
  • 如果姑娘大于18岁小于25岁,打印“心动表白”
  • 如果姑娘大于25岁小于45岁,打印“阿姨好”
  • 如果姑娘大于45岁,打印“奶奶好”
  1. while True:
  2.     girl_age =int( input('输入美女年龄'))
  3.     if girl_age < 18:
  4.         print('不接受未成年')
  5.     elif girl_age >= 18 and girl_age < 25:
  6.         print('心动表白')
  7.     elif girl_age >= 25 and girl_age < 45:
  8.         print('阿姨好')
  9.     else :
  10.         print('奶奶好')
复制代码
3.预习while循环,打印1-100之间的偶数:
  1. i = 1
  2. while i < 101:
  3.     oi = i % 2
  4.     if oi == 0:
  5.         print(i)
  6.     i += 1
复制代码
4.通过预习写一个猜年龄游戏,需求:给定一个标准年龄,用户通过输入年龄判断年龄是否等于标准年龄,如果等于——打印猜对了;如果小于——打印猜小了;如果大于——打印猜大了,增加用户输入年龄功能,并可以参考while循环博客,为应用程序添加循环。

预习while循环,猜年龄游戏升级版,有以下三点要求:

  • 允许用户最多尝试3次
  • 每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
  • 如果猜对了,就直接退出
  1. age = 25
  2. count = 0
  3. while count < 4:
  4.     user_age = int(input('请输入你的年龄:'))
  5.     if user_age == age:
  6.         print('你猜对了')
  7.         break
  8.     elif user_age > age:
  9.         print('你猜大了')
  10.     else:
  11.         print('你猜小了')
  12.     count += 1
  13.     if count != 3 :
  14.         continue
  15.     else:
  16.         print('你已经猜了三次,答"Y"或者"y"还想再玩,答"N"或者"n"退出')
  17.         user_player = input('请输入:')
  18.         if user_player == "Y" or user_player == "y":
  19.             count = 0
  20.         else:
  21.             break
复制代码
5.统计s = 'hello alex alex say hello sb sb'中每个单词的个数
  1. s = 'hello alex alex say hello sb sb'
  2. l=s.split()
  3. print(l)
  4. dic = {}
  5. for item in l:
  6.     if item in dic:
  7.         dic[item]=dic[item]+1
  8.     else:
  9.         dic[item]=1
  10. print(dic)
复制代码
6.统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前10个单词及其出现次数
  1. from collections import Counter
  2. import re
  3. with open('a.txt', 'r', encoding='utf-8') as f:
  4.     txt = f.read()
  5. c = Counter(re.split('\W+',txt))  #取出每个单词出现的个数
  6. print(c)
  7. ret = c.most_common(10)   #取出频率最高的前10个
  8. print(ret)
复制代码
7.冒泡排序
  1. def mao_pao(li):
  2.     for i in range(len(li)):
  3.         for j in range(len(li)):
  4.             if li[i] < li[j]:
  5.                 li[i],li[j] = li[j] ,li[i]
  6. import random
  7. li = list(range(10))
  8. random.shuffle(li)
  9. print(li)
  10. mao_pao(li)
  11. print(li)
复制代码
8.删除列表中的重复元素
  1. #方式一
  2. li = [1,5,5,4,12,3,1,5]
  3. print(list(set(l)))
  4. #方式二
  5. li = [1,5,5,4,12,3,1,5]
  6. def func(li):
  7.     l = []
  8.     for i in li:
  9.         if i not in l:
  10.             l.append(i)
  11.     return l
  12. print(func(li))
复制代码
9.二分查找

[code]#方式一:递归版li = [1,2,3,4,5,6,7,8,9,10]def erfen(li,aim ,start=0 ,end=len(li)-1):    if start aim :  #如果中间的值比目标值大,就从左边找            return erfen(li,aim,start,mid-1)        elif li[mid]

举报 回复 使用道具