1, Output 1 to 100 of even
#!/usr/bin/env python#-*-coding:utf-8-*-#定义初始值start =1while True: #判断start的值若其为51, the last time you have output 100, jump out of the loop if start= =51: break print start*2 start +=1
2, Output 1 to 100 of the odd
#!/usr/bin/env python#-*-coding:utf-8-*-#定义初始值start =1while True: if start==100: break #% operation is take remainder if start%2 ==1: print start start +=1
3, the output 1-2+3-4+...+99-100 and
The main idea is to judge the sign in front of start--using conditional statements to determine whether it is odd or even
| the symbol before the variable |
Odd |
even |
| Start |
1 |
-1 |
Code implementation:
#!/usr/bin/env python#-*-coding:utf-8-*-#定义初始值, sum refers to the sum, and start refers to an integer of 1-100 sum=0start=1while True: if start==101 : The break #% operation takes the remainder, judging whether it is odd or even if start%2 ==1: sum=sum+start if start%2 ==0: sum=sum-start
4, user login three times retry
The main implementations are:
- Set the initial user name and login password;
- Enter the login page to remind the user to enter user name and password;
- If the user name is wrong, re-input, if the user login password three times to start again enter the user name and user login password;
Code implementation:
#!/usr/bin/env python#-*-coding:utf-8-*-#先设定初始用户名和登录密码init_usrname =raw_input (' Please enter initial username: ') init _password=raw_input (' Please enter initial password: ') #打印输出设置好的用户名和初始登录密码print (init_usrname) print (Init_password) # Enter login to meet, flag0 refers to the number of errors entered password #flag1 refers to the login success flag bit flag0=0flag1=0print (' >>>user login<<< ') while true:# Prompt user to enter user name Usr=raw_input (' Enter username: ') if Usr==init_usrname: #输入用户名正确则进入到输入登录密码阶段 #判断输错登录密码次数 W Hile flag0<3:password=raw_input (' Enter password: ') if Password==init_password: #若密码输入 Correct login Success thus jumping out of loop print (' Success login! ') Flag1=1 break Else: #计算输错次数, flag plus one flag0 +=1 if f per error Lag0<=2:print (' Wrong password,enter again! ') #输错三次跳出输入登录密码环节重新进行用户名的输入, the corresponding flag will also be zeroed if Flag1==1:break flag0=0 print (' You have tried t Hree times,login again! ') Else Print (' Wrong username,enter again! ')
Timing Effect:
End
Python Learning-A few simple small programs