欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python简单算法的实例分析

程序员文章站 2022-03-11 16:25:02
练习1将[1,2,3],[4,5,6],[7,8,9]转换成[1,2,3,4,5,6,7,8,9]lst=[]new_lst=[]count=0for i in range(1,10): lst.append(i) count+=1 if count==3: new_lst.append(lst) count=0 lst=[]print("new_lst",new_lst)lst = [i for i in range(...

练习1
将[1,2,3],[4,5,6],[7,8,9]
转换成[1,2,3,4,5,6,7,8,9]

lst=[] new_lst=[] count=0 for i in range(1,10): lst.append(i) count+=1 if count==3: new_lst.append(lst) count=0 lst=[] print("new_lst",new_lst) lst = [i for i in range(1,10)] print("lst",lst) 

python简单算法的实例分析
练习2
统计一段数字字母中的,数字数量,字母数量,和下划线数量。

 i=0 shuzi=0 zimu=0 xiahuaxian=0 while i<13: z=s[i] print(z) if z.isalpha()==True: zimu+=1 elif z.isdigit()==True: shuzi+=1 elif z.startswith(' ')==True: xiahuaxian+=1 i+=1 print(f'数字:{shuzi},字母:{zimu},下划线:{xiahuaxian}') 

python简单算法的实例分析
练习3
和电脑进行石头,剪刀,布,的游戏

 y=2 z=3 if x: a=y else: a=z print(x) x=0 y=2 z=3 a=y if x else z print(a) player=int(input('请输入您要输入的拳头 石头/(1) 剪刀/(2)布/(3):')) computer=1 print('玩家输出的拳头是%s,电脑出的拳头是%s'%(player,computer)) if(player==1 and computer==2)\ or(player==2 and computer==3)\ or(player==3 and computer==1): print('电脑弱爆了..') elif player==computer: print('平局。。。') else: print('电脑赢了。。。') 

python简单算法的实例分析
练习4
判断成绩是否合格

 if achievement>90: print("优秀") if achievement>=60and achievement<80: print("及格") if achievement<60: print("不及格") 

python简单算法的实例分析
练习5
判断时候可以进入网咖

age=int(input("请输入你的年龄:")) if age>=18: print("可以进入网咖") if age<18: print("不可以进入网咖") 

python简单算法的实例分析

本文地址:https://blog.csdn.net/kangbazi6/article/details/108841696