Python总结1
(1)1+2+3+......+99+100算法:
1 number_1=1 2 summation=0 3 print('1+2+3+......+99+100=') 4 while number_1<=100: 5 summation=summation+number_1 6 number_1=number_1+1 7 print(summation)
(2)1到100 内的奇偶数:
#算法1 odd_number=1 print('1到100内所有的奇数为:') while odd_number<=100: print(odd_number) odd_number+=2 even_number=2 print('1到100内所有的偶数为:') while even_number<=100: print(even_number) even_number+=2 #算法2 natural_number=1 print('1到100内所有的奇数为:') while natural_number<=100: if natural_number%2==1: print(natural_number) natural_number+=1 else : natural_number+=1 natural_number=1 print('1到100内所有的偶数为:') while natural_number<=100: if natural_number%2==0: print(natural_number) natural_number+=1 else : natural_number+=1
(3)用户登录程序(三次机会):
try_times=3 variate=0 while try_times>0: user_id=input('请输入你的用户名:') user_password=input('请输入你的密码:') if user_id=='野猪': if user_password=='123': print('登陆成功') try_times=0 variate=1 else: try_times-=1 print('用户名或密码错误,你还有',try_times,'次机会') else : try_times-=1 print('用户名或密码错误,你还有',try_times,'次机会') if variate==0: print('您的尝试次数已经用尽') else : pass
(4)身份证生日&性别提取:
Id_number=input('请输入你的身份证号:') print('你的生日为',Id_number[6:10],'年',Id_number[10:12],'月',Id_number[12:14],'日') if int(Id_number[-2])%2==1: print('性别:男') else : print('性别:女')
(5)1-2+3-4......+99-100算法:
#算法1 odd_number=1 even_number=-2 result_1=0 print('1-2+3-4......+99-100=') while odd_number<=100: result_1=result_1+odd_number+even_number odd_number+=2 even_number-=2 print(result_1) #算法2 number=1 result_2=0 print('1-2+3-4......+99-100=') while number<=100: result_2=result_2-1 number+=2 print(result_2)