After self-study, the paper solves several simple problems by using cyclic statements and judgment statements:
1, the realization of 1 to 10 and:
x = 1he = 0while x < one: if x = = 7: pass else: he = he + x x = x + 1print (HE)
2, the realization of 1 to 100 and:
X=1he =0while x< 101: he=x+he x=x+1print (He)
3, achieve 100 even add:
x = 1while x < 101: if x% 2 = = 0: print (x) else: pass x = x + 1
4, achieve the odd sum within 100:
x = 1while x < 101: if x% 2 = = 0: pass else: print (x) x = x + 1
5, the realization of seeking 1-2+3-4+5 ... 99 of all numbers of the and:
x = 1he = 00while x <: if x% 2 = = 0: he = He-x else: he = he + x x = x + 1print (HE)
6. Achieve three landing attempts:
Pure If statement version:
Idcard = 123passward = 123x = 0a = input ("Please enter your account") c = input ("Please enter your password") if a = = Idcard and c = = Passward: print ("Login Successful") else: print ("Login failed") a = input ("Please enter your account") c = input ("Please enter your password") if a = = Idcard and c = = Passward: prin T ("login successfully") else: print ("Login failed") a = input ("Please enter your account") c = input ("Please enter your password") if a = = Idcard and c = = Passward: print ("Successful login") else: print ("Three login failed, forced exit")
To add a while loop version:
idcared= 123passward = 123x = 0while x < 3: a = input ("Please enter your account") c = input ("Please enter your password") if a = = Idcard and C = = Passward: print ("login successfully") else: print ("Login failed Please reenter") if x==2: print ("Input three times failed, force exit") x = x + 1
A simple attempt to pass through these questions summarizes 1, often forgetting to add a loop-limiting variable. 2, do not pay attention to the amount of indentation in the code.
(i) Python simple application