01-python3.5-模塊導入-while-for-range-break-continue

1、輸入用戶名和密碼----導入getpass模塊python

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author:XZ
 4 
 5 """
 6 name = input("What's your name ?")
 7 print("Hello! " +  name)
 8 
 9 """
10 #########
11 #輸入密碼時,若是想要不可見,須要利用getpass 模塊中的 getpass方法,即:
12 import getpass
13 
14 _username = 'alex'
15 _password = 'abc123'
16 username = input( "username:")
17 # 將用戶輸入的內容賦值給 password 變量
18 password = getpass.getpass("password:")
19 
20 #getpass在pycharm中不太好用,建議使用window命令行測試如:
21 
22 # E:\Users\admin>python I:\pycharm\PycharmProjects\20180908PY\20190908\1-day1-01.py
23 # username:alex
24 # password:
25 # alex aaaa
26 # E:\Users\admin>
27 
28 if _username == username and _password == password:
29     print("Welcome user {name} login...".format(name=username))
30 else:
31     print("Invalid username or password !")
32 
33 print(username,password)

 

2、經過while循環猜年齡oop

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author:XZ
 4 
 5 age_of_oldboy = 56
 6 
 7 count = 0
 8 
 9 while count < 3:
10     guess_age = int(input("guess age:"))
11 
12     if guess_age == age_of_oldboy:
13         print("yes, you get it.")
14         break
15     elif guess_age > age_of_oldboy:
16         print("Think smaller!")
17     else:
18         print("Think bigger!")
19     count +=1
20 else:
21     print("you have tried too many times.. fuck off")

 

3、while循環打印到1億的數字測試

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author:XZ
 4 
 5 count = 0
 6 
 7 while True:
 8     print("count:",count)
 9     count = count + 1   #count +=1
10     if count == 100000000:
11         break

 

4、range循環打印0-9的數字spa

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author:XZ
4 
5 for i in range(10):
6     print("loop",i)

 

5、經過range設置猜年齡遊戲3次猜錯強制退出命令行

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author:XZ
 4 
 5 age_of_oldboy = 56
 6 
 7 
 8 for i in range(3):
 9     guess_age = int(input("guess age:"))
10 
11     if guess_age == age_of_oldboy:
12         print("yes, you get it.")
13         break
14     elif guess_age > age_of_oldboy:
15         print("Think smaller!")
16     else:
17         print("Think bigger!")
18 else:
19     print("you have tried too many times.. fuck off")

 

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author:XZ
 4 
 5 age_of_oldboy = 56
 6 
 7 count = 0
 8 
 9 while count < 3:
10     guess_age = int(input("guess age:"))
11 
12     if guess_age == age_of_oldboy:
13         print("yes, you get it.")
14         break
15     elif guess_age > age_of_oldboy:
16         print("Think smaller!")
17     else:
18         print("Think bigger!")
19     count +=1
20     if count == 3:
21         countine_confirm = input("Do you want to keep guessing ?")
22         if countine_confirm != "n":
23             count = 0
24 else:
25     print("you have tried too many times.. fuck off")

 

 

 

6、經過range循環打印10之內的偶數code

1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3 #Author:XZ
4 for i in range(0,11,2):
5     print("loop",i)

 

7、循環中break和continue的使用orm

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author:XZ
 4 
 5 for i in range(1,10):
 6     if i < 3:
 7         print("loop",i)
 8     else:
 9         continue
10     print("hehe...")
11 #continue 跳出本次循環,進入下一次循環
12 #break 結束整個循環

 

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 #Author:XZ
 4 
 5 for i in range(10):
 6     print('------------',i)
 7     for j in range(10):
 8         print(j)
 9         if j > 5:
10             break
相關文章
相關標籤/搜索