1. 程式人生 > >while 循環 及 and or not 練習

while 循環 及 and or not 練習

pass 登錄失敗 遊戲 過多 false 正常 輸入 為什麽 pytho

一 while 循環

語法:

while 條件:

  結果

  如果條件為真,則直接執行結果,然後再次判斷條件,直到條件是假,停止循環

結束循環:

1.改變條件

2.break

二 流程控制 break 和 continue

1.break:立刻跳出循環,打斷的意思

2.continue :停止本次循環,繼續執行下一次循環

三 邏輯運算

1.and 兩端都為真 才是真 和 or 相反

2.or 一端為真 則為真 非0 即它

3. not 非 非真即假,非假即真

運算順序:

() > not > and > or

1.判斷下面邏輯語句的 True False

print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)

# 輸出是 True
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 )

# 輸出是 False

2.求出下面邏輯語句的值

print(8 or 3 and 4 or 2 and 0 or 9 and 7)

#輸出結果是  8

print(0 or 2 and 3 and 4 or 6 and 0 or 3)

#輸出結果是  4

3.下列結果是什麽

print(6 or 2 > 1)

# 輸出結果是 6
print(3 or 2 > 1)

# 輸出結果是 3
print(0 or 5 < 4)

# 輸出結果是 False
print(5 < 4 or 3)

# 輸出結果是 3
print(2 > 1 or 6)

# 輸出結果是 True
print(3 and 2 > 1)

# 輸出結果是 True
print(0 and 3 > 1)

# 輸出結果是 0
print(2 > 1 and 3)

# 輸出結果是 3
print(3 > 1 and 0)

# 輸出結果是 0 print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2) # 輸出結果是2

4、while循環語句基本結構?

while 條件:

  循環體

else: 循環正常結束後會執行這裏

5、利用while語句寫出猜大小的遊戲:
設定一個理想數字比如:66,讓用戶輸入數字,如果比66大,則顯示猜測
的結果大了;如果比66小,則顯示猜測的結果小了;只有等於66,顯示猜測結果
正確,然後退出循環。

s = 66
while True:
    num = int(input("輸入一個你猜的數字:"))
    if num > s:
        print("大了")
    elif num < s:
        print("小了")
    else:
        print("恭喜你猜對了")
        break

6、在5題的基礎上進行升級:
給用戶三次猜測機會,如果三次之內猜測對了,則顯示猜測正確,退出循
環,如果三次之內沒有猜測正確,則?動退出循環,並顯示‘太笨了你....’。

count = 1
while count <= 3:
    num = int(input("輸入一個你猜的數字吧:"))
    if num > 66:
        print("大了,大了,再小點")
    elif num < 66:
        print("小了,小了,再大點")
    count += 1
else:
    print("這個都猜不出來,太笨了吧....")

7.使用while循環輸入 1 2 3 4 5 6 8 9 10

###  寫法 1 
count = 1 while count <= 10: if count == 7: count += 1 print(count) else: print(count) count += 1
## 寫法 2 count = 1 while count < 11: if count == 7: count += 1 continue else: print(count) count += 1

## 寫法 3 count = 1 while count <= 10: if count != 7: print(count) count += 1

8.求1-100的所有數的和

sum = 0
count = 1
while count <= 100:
    sum += count
    count += 1
print(sum)

9.輸出 1-100 內的所有奇數

## 第一種寫法

count = 1
while count <= 100:
    if count % 2 == 1:
        print(count)
    count += 1

# 第二種寫法

count = 1
while count <= 100:
    print(count)
    count += 2

10.輸出 1-100 內的所有偶數

# 寫法 1 
count = 2
while count <= 100:
    print(count)
    count += 2

## 寫法 2
count = 1
while count <= 100:
    if count % 2 == 0:
        print(count)
    count += 1

11.求1-2+3-4+5 ... 99的所有數的和.

# 寫法 1
sum = 0
count = 1
while count < 100:
    if count % 2 == 1:
        sum += count
        count += 1
    else:
        sum -= count
        count += 1
print(sum)

## 寫法 2
sum = 0
count = 1
while count < 100:
    if count % 2 == 1:
        sum += count
    else:
        sum -= count
    count += 1
print(sum)

### 看上面代碼 註意看,差別在什麽地方,為什麽會出現這樣的 結果???

12.用戶登陸(三次輸錯機會)且每次輸錯誤時顯示剩余錯誤次數(提示:使用字符串格式)

count = 1
while count <= 3:
    username = input("輸入你的賬號:")
    password = input("輸入你的密碼:")
    if username == "alex" and password == "123456":
        print("登陸成功")
    else:
        print("登錄失敗,你還有%s次機會" % (3 - count))
        print(f"登錄失敗,你還有{3 - count}次機會")
    count += 1

13. 用戶輸入一個數. 判斷這個數是否是個質數(升級題).

num = int(input("輸入你要判斷的數字:"))
if num == 1:
    print("這個我也不知道了")
else:
    count = 2
    while count < num - 1:
        if num % count == 0:
            print("這個不是質數")
            break
        count += 1
    else:
        print("恭喜你,找到了一個質數")


####   循環 判斷,  主動 退出
while 1:
num = int(input("輸入你要判斷的數字,輸入0退出:"))
if num == 0:
print("退出中...")
break
elif num == 1:
print("我也不知道這個是不是,因為質數定義在大於1的基礎上的")
else:
count = 2
while count <= num - 1:
if num % count == 0:
print("不是質數")
break
count += 1
else:
print("恭喜你找到了一個質數,再見了,朋友")
break

14. 輸入一個廣告標語. 判斷這個廣告是否合法. 根據最新的廣告法來判斷. 廣
告法內容過多. 我們就判斷是否包含‘最‘, ‘第一‘, ‘稀缺‘, ‘國家級‘等字樣. 如果包
含. 提示, 廣告不合法
例如, 1. 老男孩python世界第一. ==> 不合法
2. 今年過年不收禮啊. 收禮只收腦白金. ==> 合法

while True:
    content = input("輸入你的廣告語,輸入Q退出:")
    if content.upper() == "Q":
        print("退出啦,再見...")
        break

    elif "" in content or "第一" in content or "稀缺" in content or "國家級" in content:
        print("不合法,請重新輸入")
        continue
    else:
        print("廣告合法")
        break

14. 輸入一個數. 判斷這個數是幾位數(用算法實現)(升級題)

num = int(input("輸入一個你要判斷的數字:"))
count = 0
while num >= 1:
    num //= 10
    count += 1
print(count)

while 循環 及 and or not 練習