Published 9月 08, 2018 by with 0 comment

習題29 - if語句




這次是學if語句, if為程式碼創造了一個分支,
如果這句布林表達式為真, 則運行接下來的程式碼,
否則就跳過它.
用Notepad++打下列的程式碼,
另存為ex29.py. 我附上中文注釋方便好讀.
people = 20
cats = 30
dogs = 15

# 假如people(20) < cats(30), 則運行接下來的程式碼 (print("Too many cats! The world is doomed!"))
if people < cats:
    print("Too many cats! The world is doomed!")

if people > cats:
    print("Not many cats! The world is saved!")

if people < dogs:
    print("The world is drooled on!")

if people > dogs:
    print("The world is dry!")

# +=的意思就是遞增, dog = dog + 5 = 15 + 5
# 反過來 -= 就是遞減.
dogs += 5

if people >= dogs:
    print("People are greater than or equal to dogs.")

if people <= dogs:
    print("People are less than or equal to dogs.")

if people == dogs:
    print("People are dogs.")

然後用Windows的cmd, 執行python.
C:\Windows\System32>cd C:\Users\Peter\Desktop\Python\LP3THW
C:\Users\Peter\Desktop\Python\LP3THW>python ex29.py
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.
People are less than or equal to dogs.
People are dogs.

C:\Users\Peter\Desktop\Python\LP3THW>

完成


最初發表 / 最後更新: 2018.09.08 / 2018.09.08

0 comments:

張貼留言