if True: print("hello") print("world!")
輸出:dom
hello
world!函數
""" 問題:輸入一個年齡。若大於18歲,則能夠上網 重點:因爲input輸入的是字符串類型,故不能與18比較,因此要進行int類型轉換 """ age = int(input("請輸入您的年齡:")) if age > 18: print(f'你的年紀是{age},能夠上網!')
輸出:spa
請輸入您的年齡:19
你的年紀是19,能夠上網!3d
age = int(input("請輸入您的年齡:")) if age < 18: print(f'你的年紀是{age},過小了!') elif(age >= 18) and (age <= 60): print(f'您的年紀是{age},合法年紀!') elif age > 60: print(f'您的年紀是{age},太老了!')
註釋: (age >= 18) and (age <= 60) 能夠簡化爲:18 <= age <= 60code
""" money = 1,表明有錢,反之沒錢 seat = 1,表明有座,反之沒座 """ money = 1 seat = 0 if money == 1: print("有錢,請上車") if seat != 1: print(" 沒座,請站着") else: print(" 有座,趕忙坐") else: print("沒錢,後邊跑")
案例:猜拳遊戲blog
擴充:隨機數遊戲
一、導入random模塊字符串
import random二、使用函數input
random.randint(開始,結束)
""" 1、出拳 玩家:手動輸入:plater 剪刀:0、石頭:1,布:2 電腦:隨機輸入 隨機數:computer 2、判斷 玩家贏 玩家剪刀,電腦布 玩家石頭,電腦剪刀 玩家布,電腦石頭 平局 玩家剪刀,電腦剪刀 玩家石頭,電腦石頭 玩家布,電腦布 電腦贏 玩家剪刀,電腦石頭 玩家石頭,點膠布 玩家布,電腦剪刀 """ import random computer = random.randint(0,2) player = int(input("請出拳:0--剪刀,1--石頭,2--布:")) print(f'電腦:{computer}') if((player == 0) and (computer ==2 ) or (player == 1) and (computer == 0) or (player == 2) and (computer == 1)): print("玩家獲勝!") elif player == computer: print("平局") else: print("電腦獲勝")
語法:條件成立執行的表達式 if 條件 else 條件不成立執行的表達式 for循環
a = 11 b = 22 c = a + b if a < b else a - b print(c)
語法:
舉例1:1-100整數相加,而後輸出結果
""" 1-100整數相加,而後輸出結果 """ i = 1 result = 0 while i <= 100: result += i i += 1 print(f'1+....+100={result}')
舉例2:1-100內的偶數相加,而後輸出結果
方法1:
i = 0 even_result = 0 while i <= 100: even_result += i i += 2 print(f'0+2+...+100={even_result}')
方法2:
i = 1 even_result = 0 while i <= 100: if i % 2 == 0: even_result += i i += 1 print(f'0+2+...+100={even_result}')
""" j=0 j=1 j=2 j=3 i的第1遍循環 j=0 j=1 j=2 j=3 i的第2遍循環 j=0 j=1 j=2 j=3 i的第3遍循環 j=0 j=1 j=2 j=3 i的第4遍循環 i的循環結束 """ i = 0 while i <= 3: j = 0 while j <= 3: print(f'j={j}') j += 1 print(f'i的第{i+1}遍循環') i += 1 print("i的循環結束")
舉例3:
""" 打印星號:正方形 ***** ***** ***** ***** ***** """ i = 0 while i <= 4: j = 0 while j <= 4: print("*",end="") j += 1 print() i += 1
舉例4:
""" 打印星號:三角形 * ** *** **** ***** """ i = 0 while i <= 4: j = 0 while j <= i: print("*",end="") j += 1 print() i += 1
舉例5:九九乘法表
""" 九九乘法表 1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 """ i = 1 while i <= 9: j = 1 while j <= i: print(f'{i} * {j} = {i*j}',end="\t") j += 1 print() i += 1
break
終止整個循環
""" i == 4時,終止整個循環 """ i = 1 while i <= 5: if i == 4: print("結束了") break print(i) i += 1輸出:
1
2
3
結束了continue
退出當前一次循環而執行下一次循環
""" i == 4時,終止整個循環 在使用continue以前必定要修改計數器,不然陷入死循環 """ i = 1 while i <= 5: if i == 4: print("跳過") i += 1 continue print(i) i += 1輸出:
1
2
3
跳過
5
循環正常執行完畢後,才執行else的內容:
""" 5 4 3 2 1 結束了 """ i = 5 while i > 0: print(i) i -= 1 # 循環正常結束後才執行 else: print("結束了")
break的使用:
""" 5 4 3 """ i = 5 while i > 0: print(i) i -= 1 if i == 2: break # 循環正常結束後才執行 else: print("結束了")
continue的使用:
""" 5 4 3 跳過2 1 結束了 """ i = 5 while i > 0: if i == 2: i -= 1 print("跳過2") continue print(i) i -= 1 # 循環正常結束後才執行 else: print("結束了")
語法:
break:
""" for循環中使用break: h e l l o w o 遇到r結束 """ src = 'helloworld' for i in src: if i == 'r': print("遇到r結束") break print(i)continue:
""" for循環中使用continue: h e l l o w o 遇到r跳過 l d """ src = 'helloworld' for i in src: if i == 'r': print("遇到r跳過") continue print(i)
""" h e l l o w o r l d 結束循環 """ src = 'helloworld' for i in src: print(i) # for 循環正常結束後,纔會執行else else: print("結束循環")
break的使用:
""" h e l l o w o r """ src = 'helloworld' for i in src: print(i) if i == 'r': break # for 循環正常結束後,纔會執行else else: print("結束循環")
continue的使用:
""" h e l l o w o 跳過r l d 結束循環 """ src = 'helloworld' for i in src: if i == 'r': print("跳過r") continue print(i) # for 循環正常結束後,纔會執行else else: print("結束循環")