(一)python簡單應用
阿新 • • 發佈:2018-08-14
總結 限制 循環語句 簡單 light highlight true ... 添加
自學之後運用循環語句和判斷語句所解決的幾個簡單問題:
1、實現1到10的和:
x = 1 he = 0 while x < 11: if x == 7: pass else: he = he + x x = x + 1 print(he)
2、實現1到100的和:
x=1 he =0 while x< 101: he=x+he x=x+1 print(he)
3、實現100以內偶數相加:
x = 1 while x < 101: if x % 2 == 0: print(x) else: pass x = x + 1
4、實現100以內的奇數相加:
x = 1 while x < 101: if x % 2 == 0: pass else: print(x) x = x + 1
5、實現求1-2+3-4+5 ... 99的所有數的和:
x = 1 he = 00 while x < 100: if x % 2 == 0: he = he - x else: he = he + x x = x + 1 print(he)
6、實現三次登陸嘗試:
純IF語句版:
idcard = 123 passward = 123 x = 0 a = input("請輸入你的賬號") c = input("請輸入你的密碼") if a == idcard and c == passward: print("成功登陸") else: print("登陸失敗") a = input("請輸入你的賬號") c = input("請輸入你的密碼") if a == idcard and c == passward: print("成功登陸") else: print("登陸失敗") a = input("請輸入你的賬號") c = input("請輸入你的密碼") if a == idcard and c == passward: print("成功登陸") else: print("三次登陸失敗,強制退出")
添加 while循環版:
idcared= 123 passward = 123 x = 0 while x < 3: a = input("請輸入你的賬號") c = input("請輸入你的密碼") if a == idcard and c == passward: print("成功登陸") else: print("登陸失敗 請重新輸入") if x==2 : print("輸入三次失敗,強制退出") x = x + 1
經過這幾個問題的簡單嘗試總結了 1、常常會忘記添加循環的限制變量。 2、不註意代碼當中的縮進量。
(一)python簡單應用