1. 程式人生 > >小甲魚Python第四講

小甲魚Python第四講

一次 dom else 告訴 span -i asd and 一個

1while語句中,當條件為真時,它會一直循環下去,比如下面的例子,不過可以用Ctral + C來強制結束

while ‘C‘:
    print("i love you")

2.觀察打印次數

i = 10
while i > 0:
    print("i love you")
    i = i - 1

打印10次 ‘i love you’

3.註意and的用法(表示兩邊都為真),請寫出與10<a<19等價的表達式

10 < a < 19
10 < a and a < 19

4.短路邏輯

籠統的說,短路邏輯值得是在一個邏輯中,只判斷前半部分,只要前半部分可以確定結果,就不會判斷邏輯的後半部分。

5.Python3中,一行可以書寫多條語句麽?

可以;

print(‘asdf‘);print(‘asdf‘)

6.python3中,一個語句可以分成多行書寫麽?

可以,可以使用反斜杠或括號分解成幾行

print\

("hh")

7.and or

x or y ---if x is false,then y,else x

x and y --if x is false,then x,else y

not x--- if x if false, then True,else False

8.註意while中的條件,即0為假

技術分享圖片
num = int(input("請輸入一個整數:"))
i = 0
while num:
    i = i + 1
    num = num - 1
    print(i)
技術分享圖片

另附小甲魚的

技術分享圖片
temp = input(‘請輸入一個整數:‘)
number = int(temp)
i = 1
while number:
    print(i)
    i = i + 1
    number = number - 1
技術分享圖片

9.(抄襲小甲魚的)

技術分享圖片
temp = input(‘請輸入一個整數:‘)
number = int(temp)
while number:
    i = number - 1
    while i:
        print(‘ ‘, end = ‘‘)
        i = i - 1
    j = number
    while j:
        print(‘*‘, end = ‘‘)
        j = j - 1
    print()
    number = number - 1
技術分享圖片

10.(抄襲小甲魚的)

技術分享圖片
import random
times = 3
secret = random.randint(1,10)
print(‘------------------我愛魚C工作室------------------‘)
# 這裏先給guess賦值(賦一個絕對不等於secret的值)
guess = 0
# print()默認是打印完字符串會自動添加一個換行符,end=" "參數告訴print()用空格代替換行
# 嗯,小甲魚覺得富有創意的你應該會嘗試用 end="JJ"?
print("不妨猜一下小甲魚現在心裏想的是哪個數字:", end=" ")
while (guess != secret) and (times > 0):
    temp = input()
    guess = int(temp)
    times = times - 1 # 用戶每輸入一次,可用機會就-1
    if guess == secret:
        print("我草,你是小甲魚心裏的蛔蟲嗎?!")
        print("哼,猜中了也沒有獎勵!")
    else:
        if guess > secret:
            print("哥,大了大了~~~")
        else:
            print("嘿,小了,小了~~~")
        if times > 0:
            print("再試一次吧:", end=" ")
        else:
            print("機會用光咯T_T")
print("遊戲結束,不玩啦^_^")

小甲魚Python第四講