本文主要是介绍Preparatory Class-Day5----- 循环(几个小程序),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# 1. 询问年龄,性别,如果是10-12岁的小女孩,则邀请加入足球队;询问10次,输出满足条件总人数
def que1():def judge_player(sex, age):if sex == '女'and 12 <= age <= 14:return True, ('--OK! --欢迎加入足球队!!!')else:if sex == '男':return False, ('--Sorry! 只招女球员')if age < 12:return False, ('--Sorry! 再过{}年再来吧'.format(12 - age))if age > 12:return False, ('--Sorry! 只招14岁以下儿童')import randomsex_list = []age_list = []sex_dic = {0:'男', 1:'女'}for i in range(10):sex_list.append(sex_dic[random.randint(0,1)])age_list.append(random.randint(8, 18))result = []for interviewer in range(10):agei = age_list[interviewer]sexi = sex_list[interviewer]print(type(age_list[interviewer]))print('第{}位采访者,性别:【{}】,年龄【{}】 '.format(str(interviewer+1).zfill(2), sexi, str(agei).zfill(2))\+ judge_player(sexi, agei)[1])result.append(judge_player(sex_list[interviewer], age_list[interviewer])[0])print('\n满足条件的总人数为:{}'.format(result.count(True)))
# que1()# 2. 排序法
l = [6,96,8,9,15,85,2, 87, 1]
def que2():# 冒泡排序def bubbleSort(l):# 循环次数 ------------------------1, len-1次for time in range(1, len(l)):# 对比点 = 总长度 - 循环次数 ------0, len-time位置# print(time)for index in range(len(l)-time):if (l[index] > l[index + 1]):l[index], l[index+1] = l[index+1], l[index]# print('{}---{}---{}~~~~{}'.format(l, time, index, index+1))return l# 直接选择排序def selectSort(m):# time为比对基准元素位置,------------------ 0 - len-1for time in range(len(l)-1):# 比对的起始点 = 基准点time + 1 ------- time+1 - lenfor index in range(time+1, len(l)):# print('{}---{}'.format(time, index))if (l[time] > l[index]):l[time], l[index] = l[index], l[time]# print(time, index)return m# 插入排序----抽扑克牌def insertSort(m):result = []# 单个元素k插入列表lidef to_insert(li, k):# 标识符tab = False# 寻找插入位置# 循环次数应该至少大于列表长度+1,None也占一位(空列表),即认为扑克牌最末尾还有一张‘空牌’for i in range(len(li)+1):# 修改标识符,标志‘遍历完后的下一个循环’,即在和‘空牌’比较if i == (len(li)):tab = True# 如果在对li[-1]比较完成(包含)之前,且寻找到位置,即把扑克从左往右比较一遍if not tab and k < li[i]:li.insert(i, k)break# 如果遍历完成,多循环一次,即和‘空牌’不需要比较,直接把牌替换掉‘空牌’if tab:li.append(k)return li# 遍历列表# result = result[:1]for length in range(len(m)):result = to_insert(result, m[length])# print(result,m[length])return resultprint('插入排序:',insertSort(l))print('选择排序:',selectSort(l))print('冒泡排序:',bubbleSort(l))
que2()# 3.判断回文数
def que3():# 中间向两端判断def judge_palindrome1(to_judge):import mathlen_to = (len(to_judge) - 1)/2len_to_right = int(math.ceil(len_to + 0.5)) #向上取整)len_to_left = int(math.floor(len_to - 0.5)) #向下取整result = Truefor i in range(int(len_to_left)+1):# print(to_judge[len_to_left - i], to_judge[len_to_right + i])if to_judge[len_to_left - i] != to_judge[len_to_right + i]:result = Falsebreakreturn result# 两端向中间判断def judge_palindrome2(to_judge):mid = (len(to_judge)-1)/2result = Truefor gap in range(len(to_judge)):# print(gap, -1- gap, to_judge[gap], to_judge[-gap-1])if float(gap) > mid:breakelif to_judge[gap] != to_judge[-gap-1]:result = False# else:# print(float(gap) > mid)return resultto_judge = input('请输入需要判断的字符串:\n')print('从中间向两端判断方法,【{}】的回文数判定结果:{}'.format(to_judge, judge_palindrome2(to_judge)))print('从两端向中间判断方法,【{}】的回文数判定结果:{}'.format(to_judge, judge_palindrome2(to_judge)))
# que3()# 4.打印三角形
def que4():# 实心三角形def triangle_solid(num):for i in range(num):tab = Falsefor j in range(i+1):print('*',end='')if j == i:tab = Trueif tab:print('\n' ,end = '')# 空心三角形def hollow_solid(num):for i in range(num):tab = Falsefor j in range(i + 1):# 判断是否最后一行if i != num-1:# 循环完成,修改标识符if j == i :tab = True# 判断打印空格还是*if (i == j or j == 0):print('*',end='')else :print(' ',end='')# 最后一行,全部打印星号else:print('*', end='')if tab:print('\n', end='')num = int(input('你想打印几边形?请输入\n'))triangle_solid(num)print('\n')hollow_solid(num)
# que4()# 5.输出乘法表
def triangle_solid():print('九九乘法表如下:')def deal_str(s):if len(s) == 2:return selse:return ' ' + sfor line in range(0,9):tag = Falsefor index in range(0, 9):# print(line+1, index+1)# if index != line:# print('\t\t', end='')if index >= line:result = deal_str(str((line+1)*(index+1)))print('{}×{} = {}'.format(line+1, index+1, result), end='\t')# print(line+1, '*', +index+1, end='\t\t\t')else:print('\t\t\t', end='')if index == 8:tag = Trueif tag:print('\n')
# triangle_solid()# 6. 猜数字
def guess():import randomdef judge_num(num, num_random):if num > num_random:print('It\'s too big')return 1elif num < num_random:print('It\'s too small')return 1else:print("Congratulation!! That\' right!")return 0# 产生随机数num_start = int(input('Digital lower limit of guess number:\n'))num_end = int(input('Digital upper limit of guess number:\n'))num_random = random.randint(num_start, num_end)# 参数初始化result = 1 # 判断结果i = 0 # 当前循环次数frequency = 3 # 循环限制次数# 提示总猜测次数、剩余次数print('Notice: You have【{}】 chances you guess '.format(frequency), end = '--&&>>--')# print('【{}】 chances left now:\n'.format(frequency - i +1))while result and i != frequency:# 猜数字print('【{}】 chances left now:\n'.format(frequency - i))num = int(input('Please guess a int_number:\n'))result = judge_num(num, num_random)i += 1print('Game Over')
# 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 OverProcess finished with exit code 0
这篇关于Preparatory Class-Day5----- 循环(几个小程序)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!