1. 程式人生 > >python 練習1(流控制)

python 練習1(流控制)

控制 pos 用戶 str 輸入 pri bin imp else

#!/usr/bin/python #_*_ coding:utf-8 _*_

#練習題 #1、使用while循環輸入 1 2 3 4 5 6 8 9 10 #a.定義一個變量存放數字 #b.用while判斷大於10結束 test1=1 while test1<=10: print(test1) test1+=1
#2、求1-100的所有數的和 #a.定義一個變量存放和,另外一個變量存放數字 #b.while 循環加 test2=1 count1=0 while test2<=100: count1=count1+test2 test2 +=1 print(count1)
#3、輸出 1-100 內的所有奇數 #a.定義一個變量存放數字 #b.while 循環取2的余數,如果不為0是奇數 test3=1 while test3<=100: tf=test3%2 if tf==0: pass else: print(test3) test3 +=1
#4、輸出 1-100 內的所有偶數 test4=1 while test4<=100: tf=test4%2 if tf==0: print(test4) test4 +=1
#5、求1-2+3-4+5 ... 99的所有數的和 #a.判斷數字寄數用加號,是偶數用減號 #b.輸了等式和結果 test5=2 stest=‘1‘ count2=1 while test5<=99: tf=test5%2 if tf == 0: count2=count2 + test5 stest=stest+‘-‘+test5.__str__() else: count2=count2 - test5 stest=stest+‘+‘+test5.__str__() test5 +=1
print(‘{0}={1}‘.format(stest,count2)) #6、用戶登陸(三次機會重試) #a.定義三個變量 #b.獲取用戶輸入名和密碼 #c.判斷是否相等,記不等次數
import getpass name=‘‘ passwd=‘‘ n=1 while n<=3: name=input(‘請輸入用戶名:‘) passwd=getpass.getpass(‘請輸入密碼:‘) if name== ‘yjy‘ and passwd == ‘123‘: print(‘恭喜你,登錄成功!‘) break else: pass n +=1

python 練習1(流控制)