python_chapter_5

Ture 和 False的測試
if語句 if_else語句 if-elif-else語句

# if語句基本用法

cars = ['bmw', 'audi', 'subaru', 'toyota']
for car in cars:
    if car == 'bmw':
        print(car.upper())
    else:
        print(car.title())

#
# 檢查是否在列表裏
banned_users = ['A', 'B', 'C']
user = "wenzheng"
if user not in banned_users:
    print(user.title() + ", you can post a response if you wish ")

#
# 檢查是否在列表裏
game_active = True
can_edit = False

#
# 使用 == 返回結果 真爲True
car = 'subaru'
print("Is car == 'subaru' ? I predict True")
print(car == 'subaru')

#
# 使用 == 返回結果 真爲True
age = 12
if age < 4:
    price = 0
elif age < 18:
    price = 4
else:
    price = 10
print("Your admission cost is $" + str(price) + ".")

# 動手試一試
alien_color = 'yellow'
if alien_color == 'green':
    print("You get 5 points!")
elif alien_color == 'yellow':
    print("You get 10 points!")
elif alien_color == 'red':
    print("You get 15 points!")

alien_color = 'green'
if alien_color == 'green':
    print("You get 5 points!")
elif alien_color == 'yellow':
    print("You get 10 points!")
elif alien_color == 'red':
    print("You get 15 points!")

alien_color = 'red'
if alien_color == 'green':
    print("You get 5 points!")
elif alien_color == 'yellow':
    print("You get 10 points!")
elif alien_color == 'red':
    print("You get 15 points!")

# 5-8
lists = ['admin', 'Alice', 'wenzheng', 'Meng', 'Lihua']
for list_name in lists:
    if list_name == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello " + list_name + ",thank you logging in again")

if len(lists) == 0:
    print("We need to find some users!")
else:
    n = len(lists)
    del lists[-n:]  # 大神的刪除數列表所有元素的方法
    print(lists)
    print("We need to find some users!")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章