几个小程序

# 1. 询问年龄,性别,如果是10-12岁的小女孩,则邀请加入足球队;询问10次,输出满足条件总人数

 1 def que1():
 2     def judge_player(sex, age):
 3         if sex == ''and 12 <= age <= 14:
 4             return True, ('--OK!     --欢迎加入足球队!!!')
 5         else:
 6             if sex == '':
 7                 return False, ('--Sorry!  只招女球员')
 8             if age < 12:
 9                 return False, ('--Sorry!  再过{}年再来吧'.format(12 - age))
10             if age > 12:
11                 return False, ('--Sorry!  只招14岁以下儿童')
12     import random
13     sex_list = []
14     age_list = []
15     sex_dic = {0:'', 1:''}
16 
17     for i in range(10):
18         sex_list.append(sex_dic[random.randint(0,1)])
19         age_list.append(random.randint(8, 18))
20 
21     result = []
22     for interviewer in range(10):
23         agei = age_list[interviewer]
24         sexi = sex_list[interviewer]
25         print(type(age_list[interviewer]))
26         print('第{}位采访者,性别:【{}】,年龄【{}】 '.format(str(interviewer+1).zfill(2), sexi, str(agei).zfill(2))\
27               + judge_player(sexi, agei)[1])
28         result.append(judge_player(sex_list[interviewer], age_list[interviewer])[0])
29 
30     print('\n满足条件的总人数为:{}'.format(result.count(True)))
31 # que1()

# 2. 排序法

 1 l = [6,96,8,9,15,85,2, 87, 1]
 2 def que2():
 3     # 冒泡排序
 4     def bubbleSort(l):
 5         # 循环次数    ------------------------1,   len-1次
 6         for time in range(1, len(l)):
 7             # 对比点 = 总长度 - 循环次数  ------0,   len-time位置
 8             # print(time)
 9             for index in range(len(l)-time):
10                 if (l[index] > l[index + 1]):
11                     l[index], l[index+1] = l[index+1], l[index]
12                 # print('{}---{}---{}~~~~{}'.format(l, time, index, index+1))
13         return l
14 
15     # 直接选择排序
16     def selectSort(m):
17         # time为比对基准元素位置,------------------         0 - len-1
18         for time in range(len(l)-1):
19             # 比对的起始点 = 基准点time + 1 -------    time+1 - len
20             for index in range(time+1, len(l)):
21                 # print('{}---{}'.format(time, index))
22                 if (l[time] > l[index]):
23                     l[time], l[index] = l[index], l[time]
24                 # print(time, index)
25         return  m
26 
27     # 插入排序----抽扑克牌
28     def insertSort(m):
29         result = []
30 
31         # 单个元素k插入列表li
32         def to_insert(li, k):
33             # 标识符
34             tab = False
35 
36             # 寻找插入位置
37             # 循环次数应该至少大于列表长度+1,None也占一位(空列表),即认为扑克牌最末尾还有一张‘空牌’
38             for i in range(len(li)+1):
39                 # 修改标识符,标志‘遍历完后的下一个循环’,即在和‘空牌’比较
40                 if i == (len(li)):
41                     tab = True
42 
43                 # 如果在对li[-1]比较完成(包含)之前,且寻找到位置,即把扑克从左往右比较一遍
44                 if not tab and k < li[i]:
45                     li.insert(i, k)
46                     break
47             # 如果遍历完成,多循环一次,即和‘空牌’不需要比较,直接把牌替换掉‘空牌’
48             if tab:
49                 li.append(k)
50             return  li
51 
52         # 遍历列表
53         # result = result[:1]
54         for length in range(len(m)):
55             result = to_insert(result, m[length])
56 
57             # print(result,m[length])
58         return  result
59 
60     print('插入排序:',insertSort(l))
61     print('选择排序:',selectSort(l))
62     print('冒泡排序:',bubbleSort(l))
63 que2()

# 3.判断回文数

 1 def que3():
 2     # 中间向两端判断
 3     def judge_palindrome1(to_judge):
 4         import math
 5         len_to = (len(to_judge) - 1)/2
 6 
 7         len_to_right = int(math.ceil(len_to + 0.5))  #向上取整)
 8         len_to_left = int(math.floor(len_to - 0.5)) #向下取整
 9 
10         result = True
11         for i in range(int(len_to_left)+1):
12             # print(to_judge[len_to_left - i], to_judge[len_to_right + i])
13             if to_judge[len_to_left - i] != to_judge[len_to_right + i]:
14                 result = False
15                 break
16         return result
17 
18     # 两端向中间判断
19     def judge_palindrome2(to_judge):
20         mid = (len(to_judge)-1)/2
21         result = True
22         for gap in range(len(to_judge)):
23             # print(gap, -1- gap, to_judge[gap], to_judge[-gap-1])
24             if float(gap) > mid:
25                 break
26             elif to_judge[gap] != to_judge[-gap-1]:
27                 result = False
28 
29             # else:
30             #     print(float(gap) > mid)
31         return  result
32 
33     to_judge = input('请输入需要判断的字符串:\n')
34     print('从中间向两端判断方法,【{}】的回文数判定结果:{}'.format(to_judge, judge_palindrome2(to_judge)))
35     print('从两端向中间判断方法,【{}】的回文数判定结果:{}'.format(to_judge, judge_palindrome2(to_judge)))
36 # que3()

# 4.打印三角形

 1 def que4():
 2     # 实心三角形
 3     def triangle_solid(num):
 4         for i in range(num):
 5             tab = False
 6             for j in range(i+1):
 7                 print('*',end='')
 8                 if j == i:
 9                     tab = True
10             if tab:
11                 print('\n' ,end = '')
12     # 空心三角形
13     def hollow_solid(num):
14         for i in range(num):
15             tab = False
16             for j in range(i + 1):
17                 # 判断是否最后一行
18                 if i != num-1:
19                     # 循环完成,修改标识符
20                     if j == i :
21                         tab = True
22                     # 判断打印空格还是*
23                     if (i == j or j == 0):
24                         print('*',end='')
25                     else :
26                         print(' ',end='')
27                 # 最后一行,全部打印星号
28                 else:
29                     print('*', end='')
30             if tab:
31                 print('\n', end='')
32     num = int(input('你想打印几边形?请输入\n'))
33     triangle_solid(num)
34     print('\n')
35     hollow_solid(num)
36 # que4()

# 5.输出乘法表

 1 def triangle_solid():
 2     print('九九乘法表如下:')
 3 
 4     def deal_str(s):
 5         if len(s) == 2:
 6             return s
 7         else:
 8             return ' ' + s
 9 
10     for line in range(0,9):
11         tag = False
12         for index in range(0, 9):
13             # print(line+1, index+1)
14             # if index != line:
15             #     print('\t\t', end='')
16             if index >= line:
17                 result = deal_str(str((line+1)*(index+1)))
18 
19                 print('{}×{} = {}'.format(line+1, index+1, result), end='\t')
20                 # print(line+1, '*',  +index+1, end='\t\t\t')
21             else:
22                 print('\t\t\t', end='')
23             if index == 8:
24                 tag = True
25 
26             if tag:
27                 print('\n')
28 # triangle_solid()

# 6. 猜数字

 1 def guess():
 2     import random
 3 
 4     def judge_num(num, num_random):
 5         if num > num_random:
 6             print('It\'s too big')
 7             return 1
 8         elif num < num_random:
 9             print('It\'s too small')
10             return 1
11         else:
12             print("Congratulation!! That\' right!")
13             return 0
14 
15     # 产生随机数
16     num_start = int(input('Digital lower limit of guess number:\n'))
17     num_end = int(input('Digital upper limit of guess number:\n'))
18     num_random = random.randint(num_start, num_end)
19 
20     # 参数初始化
21     result = 1      # 判断结果
22     i = 0           # 当前循环次数
23     frequency = 3   # 循环限制次数
24 
25     # 提示总猜测次数、剩余次数
26     print('Notice: You have【{}】 chances you guess '.format(frequency), end = '--&&>>--')
27     # print('【{}】 chances left now:\n'.format(frequency - i +1))
28 
29     while result and i != frequency:
30         # 猜数字
31         print('【{}】 chances left now:\n'.format(frequency - i))
32         num = int(input('Please guess a int_number:\n'))
33         result = judge_num(num, num_random)
34         i += 1
35     print('Game Over')
36 # guess()

以下为运行结果

"D:\Preparatory class\Workspace\venv\Scripts\python.exe" "D:/Preparatory class/Workspace/Day5/作业.py"
<class 'int'>
第01位采访者,性别:【女】,年龄【15】 --Sorry!  只招14岁以下儿童
<class 'int'>
第02位采访者,性别:【女】,年龄【16】 --Sorry!  只招14岁以下儿童
<class 'int'>
第03位采访者,性别:【女】,年龄【15】 --Sorry!  只招14岁以下儿童
<class 'int'>
第04位采访者,性别:【女】,年龄【18】 --Sorry!  只招14岁以下儿童
<class 'int'>
第05位采访者,性别:【男】,年龄【09】 --Sorry!  只招女球员
<class 'int'>
第06位采访者,性别:【女】,年龄【08】 --Sorry!  再过4年再来吧
<class 'int'>
第07位采访者,性别:【男】,年龄【13】 --Sorry!  只招女球员
<class 'int'>
第08位采访者,性别:【男】,年龄【18】 --Sorry!  只招女球员
<class 'int'>
第09位采访者,性别:【男】,年龄【12】 --Sorry!  只招女球员
<class 'int'>
第10位采访者,性别:【男】,年龄【08】 --Sorry!  只招女球员

满足条件的总人数为:0
插入排序: [1, 2, 6, 8, 9, 15, 85, 87, 96]
选择排序: [1, 2, 6, 8, 9, 15, 85, 87, 96]
冒泡排序: [1, 2, 6, 8, 9, 15, 85, 87, 96]
请输入需要判断的字符串:
1234rtytyt4321
从中间向两端判断方法,【1234rtytyt4321】的回文数判定结果:False
从两端向中间判断方法,【1234rtytyt4321】的回文数判定结果:False
你想打印几边形?请输入
10
*
**
***
****
*****
******
*******
********
*********
**********


*
**
* *
*  *
*   *
*    *
*     *
*      *
*       *
**********
九九乘法表如下:
1×1 =  1     1×2 =  2    1×3 =  3    1×4 =  4    1×5 =  5    1×6 =  6    1×7 =  7    1×8 =  8    1×9 =  9    

            2×2 =  4    2×3 =  6    2×4 =  8    2×5 = 10    2×6 = 12    2×7 = 14    2×8 = 16    2×9 = 18    

                                3×3 =  9    3×4 = 12    3×5 = 15    3×6 = 18    3×7 = 21    3×8 = 24    3×9 = 27    

                                    4×4 = 16    4×5 = 20    4×6 = 24    4×7 = 28    4×8 = 32    4×9 = 36    

                                            5×5 = 25    5×6 = 30    5×7 = 35    5×8 = 40    5×9 = 45    

                                                    6×6 = 36    6×7 = 42    6×8 = 48    6×9 = 54    

                                                            7×7 = 49    7×8 = 56    7×9 = 63    

                                                                    8×8 = 64    8×9 = 72    

                                                                            9×9 = 81    

Digital lower limit of guess number:
1
Digital upper limit of guess number:
10
Notice: You have【3】 chances you guess --&&>>--【3】 chances left now:

Please guess a int_number:
5
It's too big
【2】 chances left now:

Please guess a int_number:
3
Congratulation!! That' right!
Game Over

Process finished with exit code 0


原文地址:https://www.cnblogs.com/geoffreyone/p/9899790.html