# 1、判斷下列列邏輯語句句的True,False.
# 1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 Ture
# 2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 False
# 2、求出下列列邏輯語句句的值。
# 1),8 or 3 and 4 or 2 and 0 or 9 and 7 8
# 2),0 or 2 and 3 and 4 or 6 and 0 or 3 4
# 3、下列列結果是什什么?
# 1)、6 or 2 > 1 6
# 2)、3 or 2 > 1 3
# 3)、0 or 5 < 4 False
# 4)、5 < 4 or 3 3
# 5)、2 > 1 or 6 True
# 6)、3 and 2 > 1 True
# 7)、0 and 3 > 1 0
# 8)、2 > 1 and 3 3
# 9)、3 > 1 and 0 0
# 10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 2
# 4、while循環語句句基本結構?
# while 條件:
# 代碼塊1(循環體)
# else:
# 代碼塊2
# 條件成立循環代碼塊1 條件不成立執行代碼塊2
# 5、利利⽤用while語句句寫出猜⼤大⼩小的游戲:
# 設定⼀一個理理想數字⽐比如:66,
# 讓⽤用戶輸⼊入數字,如果⽐比66⼤大,
# 則顯示猜測 的結果⼤大了了;
# 如果⽐比66⼩小,則顯示猜測的結果⼩小了了;
# 只有等於66,顯示猜測結果 正確,然后退出循環
print("猜大小游戲!")
while 1:
number = int(input("請輸入數字:"))
if number > 66:
print("你猜大了!")
elif number < 66:
print("你猜小了!")
else:
print("你猜對了")
break
# 6、在5題的基礎上進⾏行行升級:
# 給⽤用戶三次猜測機會,如果三次之內猜測對了,則顯示猜測正確,退出循環,
# 如果三次之內沒有猜測正確,則⾃自動退出循環,並顯示‘太笨了你....’。
print("你有三次機會猜數字大小")
count = 3
while count > 0:
count -= 1
num = int(input("請輸入數字"))
if num > 66:
print("你猜大了!")
elif num < 66:
print("你猜小了!")
else:
print("你猜對了!")
break
else:
print("太笨了你....")
# 7. 使⽤用while循環輸⼊ 1 2 3 4 5 6 8 9 10
num = 1
while num <= 10:
if num != 7:
print(num)
num += 1
# 8.求1-100的所有數的和
number = 1
sum = 0
while number <= 100:
sum += number
number += 1
print("從1到100的總和為%s" %(sum))
# 9.輸出 1-100 內的所有奇數
number = 1
while number <= 100:
if number % 2 == 1:
print(number)
number += 1
# 10.輸出 1-100 內的所有偶數
number = 1
while number <= 100:
if number % 2 == 0:
print(number)
number += 1
11.求1-2+3-4+5 ... 99的所有數的和.
number = 1
sum = 0
while number <= 99:
if number % 2 == 1:
sum += number
else:
sum -= number
number += 1
print(sum)
# 12.⽤用戶登陸(三次輸錯機會)且每次輸錯誤時顯示剩余錯誤次數
# (提示:使⽤用字符串格式化)
count = 3
while count > 0:
name = input("請輸入賬號:")
password = input("請輸入密碼:")
if "張飛" == name and "123456" == password:
print("登陸成功!")
break
else:
count -= 1
if "張飛" == name:
if count != 0:
print("密碼錯誤請重新輸入!您還有%s次機會" %(count))
else:
if count != 0:
print("賬號錯誤請重新輸入!您還有%s次機會" %(count))
else:
print("再見!")
# 13. ⽤用戶輸⼊入⼀一個數. 判斷這個數是否是⼀一個質數(升級題).
# 質數,又稱素數,是只能被1或者自己整除的自然數。
# 比1大但不是素數的數我們稱之為合數,1和0即非素數也非合數
# 最小的素數是2,而最大的素數並不存在,這一點歐幾里德已在其《幾何原本》中證明。
num = int(input("請輸入一個數字!"))
i = 2
if num >= 2:
while i < num:
if num % i == 0:
print("您輸入的數字%s不是質數!" % (num))
break
else:
i += 1
else:
print("您輸入的數字%s是質數!"%(num))
else:
print("輸入錯誤!")
# 14. 輸⼊入⼀一個⼴廣告標語. 判斷這個⼴廣告是否合法.
# 根據最新的⼴廣告法來判斷.
# ⼴廣 告法內容過多.
# 我們就判斷是否包含'最', '第⼀', '稀缺', '國家級'等字樣.
# 如果包含. 提示⼴廣告不不合法
string = input("請輸入廣告語:")
if '最'in string or '稀缺'in string or '國家級'in string or'第⼀'in string:
print("廣告不合法
")
# 14. 輸⼊⼀個數. 判斷這個數是⼏位數(⽤算法實現)(升級題)
num = int(input("輸入一個數:"))
i = 10
count = 1
while num != 0:
num //= i
if num > 0:
count += 1
print(count)
# 判斷1-100中所有質數
num = 2
while num < 100:
i = 2
while i < num:
if num % i == 0:
# print("您輸入的數字%s不是質數!" % (num))
break
else:
i += 1
else:
print("您輸入的數字%s是質數!"%(num))
num += 1
i = 0
count = 0
num = input("輸入一串數字判斷是幾位")
for i in num:
print(i)
count += 1
print(count)