1. 程式人生 > >python-修行第二天

python-修行第二天

obb 老師 我會 作業 else 流程 一次 end bubuko

day_2學習

一. 循環. while循環

while 條件:
代碼塊(循環體)
執行流程:

  1. 判斷條件是否為真. 如果真. 執行代碼塊
  2. 再次判斷條件是否為真......
  3. 當條件為假.執行else 跳出循環. 循環結束

1.讓計算機連續打印5次corn,每次延遲一秒:

技術分享圖片

import time
count = 1
while count <=5:
    print("corn")
    count = count + 1
    time.sleep(1)
2.讓用戶盡情的噴,按 q 退出,並且過濾掉"馬化騰"這三個字

技術分享圖片

while True :
    s = input("請開始噴:")
    if s == q:
        break  #停止當前循環
#過濾掉草泥馬
    if "馬化騰" in s : #in :在xxx中出現xx
        print("你輸入的內容不合法,不能輸出")
        continue  #停止當前本次循環,繼續執行下一次循環
    print("噴的內容是"+ s)
3.1+2+3+4+...+1000 = ?
count = 1
#準備一個變量
sum = 0
while count <=1000:
#累加到sum
    sum = sum + count #
把sum中的值(之前運算的結果)和當前的數相加 count = count + 1 print(sum)

4.輸出1-100的所有奇數?
count = 1
while count <=100:
    if count % 2 !=0:
        print(count)
    count = count + 1 

二.格式化輸出
%s: 字符串的占位符, 可以放置任何內容(數字)
%d: 數字的占位符

技術分享圖片

name = input("請輸入名字:")
age = input("請輸入你的年齡:")
hobby = input("輸入你的愛好:
") gender = input("請輸入你的性別:") #print(name + "今年" + age + "歲, 是一個老頭, 愛好是" + hobby + ", 性別:" + gender) #% s: 表示字符串的占位符 print("%s今年%s歲, 是一個老頭, 愛好是%s, 性別:%s" % (name, age, hobby, gender))

三. 運算符
邏輯運算:
and 並且的意思. 左右兩端的值必須都是真. 運算結果才是真
or 或者的意思. 左右兩端有一個是真的. 結果就是真. 全部是假. 結果才能是假
not 非的意思. 原來是假. 現在是真. 非真即假, 非假既真
break 結束循環. 停止當前本層循環
continue 結束當前本次循環. 繼續執行下一次循環

隨堂作業:

註:作業是自己做的,不保證正確,老師第二天講作業,有錯的我會更正,僅供參考!

每個作業都是圖一為運行結果圖,圖二為代碼^-^

一.判斷下列列邏輯語句的True,False.
# 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
# 2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

#1.True
#2.False

二.求出下列列邏輯語句的值。
# 1),8 or 3 and 4 or 2 and 0 or 9 and 7
# 2),0 or 2 and 3 and 4 or 6 and 0 or 3

#1. 8
#2. 4

三.下列列結果是什麽?
# 1)、6 or 2 > 1
# 2)、3 or 2 > 1
# 3)、0 or 5 < 4
# 4)、5 < 4 or 3
# 5)、2 > 1 or 6
# 6)、3 and 2 > 1
# 7)、0 and 3 > 1
# 8)、2 > 1 and 3
# 9)、3 > 1 and 0
# 10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

# 1)、6
# 2)、3
# 3)、False
# 4)、3
# 5)、true
# 6)、true
# 7)、0
# 8)、3
# 9)、0
# 10)、2

四.while循環語句基本結構?

# while 條件:
# 代碼塊(循環體)
# 執行流程:
# 1.首先判斷條件是否為真,如果為真,執行循環體;
# 2.然後繼續判斷條件是否為真...
# 3.最後當天劍為假時,跳出循環體,循環結束
五.利利?用while語句句寫出猜?小的遊戲:
# 設定一個理想數字比如:66,讓?用戶輸?入數字,如果?比66?大,
# 則顯示猜測 的結果?大了了;如果?比66?小,則顯示猜測的結果?小了了;
# 只有等於66,顯示猜測結果正確,然後退出循環。
技術分享圖片
count = 66
while count:
    s = int (input("請輸入一個數字:"))
    if s > count:
        print("猜大了")
    elif s < count:
        print("猜小了")
    else:
        print("猜對了")
        break
七.使?用while循環輸? 1 2 3 4 5 6 8 9 10
技術分享圖片
count = 1
while count <= 10:
    
    if count != 7:
        print(count)
    count = count + 1 
八.求1-100的所有數的和
技術分享圖片
count = 1
sum = 0
while count <= 100:
    sum = sum + count
    count = count + 1
print(sum)

九.輸出 1-100 內的所有奇數
技術分享圖片


count = 1
while count <= 100:
    if count % 2 != 0:
        print(count)
    count = count + 1
十.輸出 1-100 內的所有偶數
技術分享圖片


count = 1
while count <= 100:
    if count % 2 == 0:
        print(count)
    count = count + 1
十一.求1-2+3-4+5......99的所有數的和
技術分享圖片
count = 1
s1 = 0
s2 = 0
while count <= 99:
    if count % 2 != 0:
        s1 = s1 + count
    count = count + 1

    if count % 2 == 0:
        s2 = s2 + count
    count = count + 1
print(s1-s2)
十二.用戶登陸(三次輸錯機會)且每次輸錯誤時顯示剩余錯誤次數(提示:使?字符串格式化)

技術分享圖片

技術分享圖片

account = 123456
t = 3
while account :
    s = int (input("請輸入你的賬號:"))
    t = t - 1
    if s != account:
        print("對不起,你的用戶名不正確,請重新輸入一遍(你的登陸次數還剩%s次)" % (t))
    else:
        print("恭喜你,登陸成功!")
    if t == 0 and s !=account:
        print("你的登錄次數已用完,請明天再試")
        break
十三.用戶輸??個數. 判斷這個數是否是?個質數(升級題)
技術分享圖片
技術分享圖片
s = int(input("請輸入一個數:"))
if s <= 1:
    print("這不是質數")
elif s == 2:
    print("這是質數")
else:
    pn = 2
    while pn < s:
        if s % pn == 0:
            print("這不是質數")
            break
        pn += 1
    else:
        print("這是質數")
#十四.輸??個廣告標語. 判斷這個廣告是否合法. 根據最新的廣
# 告法來判斷. 廣告法內容過多. 我們就判斷是否包含‘最‘, ‘第?‘,
# ‘稀缺‘, ‘國家級‘等字樣. 如果包含. 提示, ?告不合法
# 例如: 1. ?男孩python世界第?. ==> 不合法
# 2. 今年過年不收禮啊. 收禮只收腦??. ==> 合法
 技術分享圖片 
while True :
    s = input("請輸入一個廣告:")
    if ""in s or "第一"in s or "稀缺"in s or "國家級"in s:
        print("你輸入的廣告不合法,請重新輸入")
    else:
        print("你輸入的廣告合法.")

十四.輸??個數. 判斷這個數是?位數(?算法實現)(升級題)

技術分享圖片

技術分享圖片

num = int (input("請輸入一個數:"))
w = 0
while num >= 1:
    num = num // 10
    w += 1
print("這個數是%s位數" % (w))

歡迎大佬批改作業,有錯幫忙指出.

"繼續奔跑 輸掉一切也不要輸掉微笑”

python-修行第二天