1. 程式人生 > >Python入門基礎(條件分支與循環)

Python入門基礎(條件分支與循環)

所有 普通 isp 讓其 clas 輸出 九九乘法表 -a res

一、if判斷:

1 語法一:
2 if 條件:
3     # 條件成立時執行的子代碼塊
4     代碼1
5     代碼2
6     代碼3
技術分享圖片
 1 示例:
 2 sex=female
 3 age=18
 4 is_beautiful=True
 5 
 6 if sex == female and age > 16 and age < 20 and is_beautiful:
 7     print(開始表白。。。)
 8 
 9 print(other code1...)
10 print(other code2...
) 11 print(other code3...)
示例
 1 語法二:
 2 if 條件:
 3     # 條件成立時執行的子代碼塊
 4     代碼1
 5     代碼2
 6     代碼3
 7 else:
 8     # 條件不成立時執行的子代碼塊
 9     代碼1
10     代碼2
11     代碼3
技術分享圖片
 1 sex=female
 2 age=38
 3 is_beautiful=True
 4 
 5 if sex == female and age > 16 and age < 20 and
is_beautiful: 6 print(開始表白。。。) 7 else: 8 print(阿姨好。。。) 9 10 11 print(other code1...) 12 print(other code2...) 13 print(other code3...)
示例
1 語法三:
2 if 條件1:
3     if 條件2:
4         代碼1
5         代碼2
6         代碼3
技術分享圖片
 1 sex=female
 2 age=18
 3 is_beautiful=True
4 is_successful=True 5 height=1.70 6 7 8 if sex == female and age > 16 and age < 20 and is_beautiful 9 and height > 1.60 and height < 1.80: 10 print(開始表白。。。) 11 if is_successful: 12 print(在一起。。。) 13 else: 14 print(什麽愛情不愛情的,愛nmlgb的愛情,愛nmlg啊.) 15 else: 16 print(阿姨好。。。) 17 18 19 print(other code1...) 20 print(other code2...) 21 print(other code3...)
示例
 1 語法四:
 2 if 條件1:
 3     代碼1
 4     代碼2
 5     代碼3
 6 elif 條件2:
 7     代碼1
 8     代碼2
 9     代碼3
10 elif 條件3:
11     代碼1
12     代碼2
13     代碼3
14 .......
15 else:
16     代碼1
17     代碼2
18     代碼3
技術分享圖片
 1 示例:
 2 如果成績 >= 90,那麽:優秀
 3 
 4 如果成績 >= 80且 < 90, 那麽:良好 
 5 
 6 如果成績 >= 70且 < 80, 那麽:普通
 7 
 8 其他情況:很差
 9 ‘‘‘
10 
11 score = input(‘please input your score: ‘)  # score=‘100‘
12 score = int(score)
13 
14 if score >= 90:
15     print(‘優秀‘)
16 elif score >= 80:
17     print(‘良好‘)
18 elif score >= 70:
19     print(‘普通‘)
20 else:
21     print(‘很差‘)
示例

二、while循環

1 語法:
2 while 條件:
3     代碼1
4     代碼2
5     代碼3

技術分享圖片
1 while True:
2     name=input(please input your name: )
3     pwd=input(please input your password: )
4 
5     if name == egon and pwd == 123:
6         print(login successful)
7     else:
8         print(username or password error)
示例
 1 結束while循環的兩種方式
 2 
 3 方式一:條件改為False,
 4     在條件改為False時不會立即結束掉循環,而是要等到下一次循環判斷條件時才會生效
 5         
 6     tag=True
 7     while tag:
 8         name=input(please input your name: )
 9         pwd=input(please input your password: )
10     
11         if name == egon and pwd == 123:
12             print(login successful)
13             tag=False
14         else:
15             print(username or password error)
16     
17         print(===>)
 1 方式二:while+break
 2     break一定要放在循環體內,一旦循環體執行到break就會立即結束本層循環
 3     
 4     while True:
 5         name=input(please input your name: )
 6         pwd=input(please input your password: )
 7     
 8         if name == egon and pwd == 123:
 9             print(login successful)
10             break
11         else:
12             print(username or password error)
13     
14         print(===>>>>>)
15         print(===>>>>>)
2.1、while+continue:結束本次循環,直接進入下一次循環
 1 # 示例一
 2 count=1
 3 while count < 6: #count=6
 4     if count == 4:
 5         count += 1
 6         continue
 7         
 8     print(count)
 9     count+=1
10 
11 # 示例二:
12 while True:
13     name=input(please input your name: )
14     pwd=input(please input your password: )
15 
16     if name == egon and pwd == 123:
17         print(login successful)
18         break
19     else:
20         print(username or password error)
21         # continue # 此處加continue無用
2.2、了解知識
while + else:

while 條件:
    代碼1
    代碼2
    代碼3
else:
    在循環結束後,並且在循環沒有被break打斷過的情況下,才會執行else的代碼
    
    
tag=True
while tag:
    print(1)
    print(2)
    print(3)
    # tag=False
    break
else:
    print(else的代碼)

2.3、while嵌套

 1 #語法
 2 while 條件1:
 3     while 條件2:
 4         代碼1
 5         代碼2
 6         代碼3
 7 
 8 示例一:
 9 while True:
10     name=input(please input your name: )
11     pwd=input(please input your password: )
12 
13     if name == egon and pwd == 123:
14         print(login successful)
15         while True:
16             print("""
17             0 退出
18             1 取款
19             2 轉賬
20             3 查詢
21             """)
22             choice=input(請輸入您要執行的操作:) #choice=‘1‘
23             if choice == 0:
24                 break
25             elif choice == 1:
26                 print(取款。。。)
27             elif choice == 2:
28                 print(轉賬。。。)
29             elif choice == 3:
30                 print(查詢)
31             else:
32                 print(輸入指令錯誤,請重新輸入)
33         break
34     else:
35         print(username or password error)
技術分享圖片
 1 # 示範二:
 2 tag=True
 3 while tag:
 4     name=input(please input your name: )
 5     pwd=input(please input your password: )
 6 
 7     if name == egon and pwd == 123:
 8         print(login successful)
 9         while tag:
10             print("""
11             0 退出
12             1 取款
13             2 轉賬
14             3 查詢
15             """)
16             choice=input(請輸入您要執行的操作:) #choice=‘1‘
17             if choice == 0:
18                 tag=False
19             elif choice == 1:
20                 print(取款。。。)
21             elif choice == 2:
22                 print(轉賬。。。)
23             elif choice == 3:
24                 print(查詢)
25             else:
26                 print(輸入指令錯誤,請重新輸入)
27     else:
28         print(username or password error)
示例二
2.4、while練習
 1 #1. 使用while循環輸出1 2 3 4 5 6     8 9 10
 2 #2. 求1-100的所有數的和
 3 #3. 輸出 1-100 內的所有奇數
 4 #4. 輸出 1-100 內的所有偶數
 5 #5. 求1-2+3-4+5 ... 99的所有數的和
 6 #6. 用戶登陸(三次機會重試)
 7 #7:猜年齡遊戲
 8 要求:
 9     允許用戶最多嘗試3次,3次都沒猜對的話,就直接退出,如果猜對了,打印恭喜信息並退出
10 #8:猜年齡遊戲升級版 
11 要求:
12     允許用戶最多嘗試3次
13     每嘗試3次後,如果還沒猜對,就問用戶是否還想繼續玩,如果回答Y或y, 就繼續讓其猜3次,以此往復,如果回答N或n,就退出程序
14     如何猜對了,就直接退出 
技術分享圖片
  1 #題一
  2 count=1
  3 while count <= 10:
  4     if count == 7:
  5         count+=1
  6         continue
  7     print(count)
  8     count+=1
  9 
 10   #2. 求1-100的所有數的和 
 11 count=1
 12 while count <= 10:
 13     if count != 7:
 14         print(count)
 15     count+=1
 16 res=0
 17 count=1
 18 while count<=100:
 19     res+=count
 20     count+=1
 21 print(res)
 22 
 23 #3. 輸出 1-100 內的所有奇數
 24 count=1
 25 while count<=100:
 26     if count%2==1:
 27         print(count)
 28     count+=1
 29 
 30 #4. 輸出 1-100 內的所有偶數
 31 count=1
 32 while count<=100:
 33     if count%2!=1:
 34         print(count)
 35     count+=1
 36 
 37 #5. 求1-2+3-4+5 ... 99的所有數的和
 38 res=0
 39 count=1
 40 while count<=99:
 41     if count%2==1:
 42         res-=count
 43     else:
 44         res+=count
 45     count+=1
 46 print(res)
 47 
 48 #6. 用戶登陸(三次機會重試)
 49 count=1
 50 while True:
 51     username = input(請輸入用戶名:)
 52     userpwd = input(請輸入用戶密碼:)
 53     if count!=3:
 54         if username==zhaokang and userpwd==123:
 55             print(登陸成功!)
 56             break
 57     else:
 58         break
 59    count+=1
 60 
 61 
 62 #7:猜年齡遊戲
 63 #要求:
 64 #    允許用戶最多嘗試3次,3次都沒猜對的話,就直接退出,如果猜對了,打印恭喜信息並退出
 65 count=1
 66 while True:
 67     age=int(input(請輸入年齡:))
 68     if count!=3:
 69         if age==18:
 70             print(猜對了)
 71             break
 72         else:
 73             print(抱歉)
 74     else:
 75         break
 76     count+=1
 77 
 78 
 79 #8:猜年齡遊戲升級版
 80 #要求:
 81 #    允許用戶最多嘗試3次
 82 #    每嘗試3次後,如果還沒猜對,就問用戶是否還想繼續玩,如果回答Y或y, 就繼續讓其猜3次,以此往復,如果回答N或n,就退出程序
 83 #    如何猜對了,就直接退出
 84 
 85 age_of_oldboy=73
 86 
 87 count=0
 88 while True:
 89     if count == 3:
 90         choice=input(繼續(Y/N?)>>: )
 91         if choice == Y or choice == y:
 92             count=0
 93         else:
 94             break
 95 
 96     guess=int(input(>>: ))
 97     if guess == age_of_oldboy:
 98         print(you got it)
 99         break
100     count+=1
答案

三、for循環

1 叠代式循環:for,語法如下

  for i in range(10):

    縮進的代碼塊

2 break與continue(同上)

3 循環嵌套

技術分享圖片
1 for i in range(1,10):
2     for j in range(1,i+1):
3         print(%s*%s=%s %(i,j,i*j),end= )
4     print()
九九乘法表

 

Python入門基礎(條件分支與循環)