1. 程式人生 > >Python程式設計入門到實踐Chapter-05

Python程式設計入門到實踐Chapter-05

"""
Exercise 5-1:
條件測試

"""

car = 'subaru'
print("Is car == 'subaru'? I predict True.")
print(car == 'subaru')

print("\nIs car == 'audi'? I predict false")
print(car == 'audi')
"""
Exercise 5-2:
更多的條件測試

"""

string1 = 'Audi'
string2 = 'audi'

print(string1 == string2)
print(string1.lower() == string2.lower())

num1 = 3
num2 = -3

print(num1 > num2)
print(num1 < num2)
print(num1 == num2)
print(num1 != num2)
print(num1 >= num2)
print(num1 <= num2)
print(num1 > 0 and num2 > 0)
print(num1 > 0 or num2 > 0)

lists = ['a', 'b', 'c']

print('a' in lists)
print('e' not in lists)
"""
Exercise 5-3:
外星人的顏色#1

"""

alien_color = 'green'

if alien_color == 'green':
    print("You just earned 5 points!")

alien_color = 'red'

if alien_color == 'green':
    print("You just earned 5 points!")
"""
Exercise 5-4:
外星人的顏色#2

"""

alien_color = 'green'

if alien_color == 'green':
    print("You just earned 5 points!")
else:
    print("You just earned 10 points!")

alien_color = 'red'

if alien_color == 'green':
    print("You just earned 5 points!")
else:
    print("You just earned 10 points!")
"""
Exercise 5-5:
外星人的顏色#3

"""

alien_color = 'green'

if alien_color == 'green':
    print("You just earned 5 points!")
elif alien_color == 'yellow' :
    print("You just earned 10 points!")
else:
    print("You just earned 15 points!")
"""
Exercise 5-6:
人生的不同階段

"""

age = 21

if age <= 2:
    print("You are a baby!")
elif age <= 4:
    print("You are a toddler!")
elif age <= 13:
    print("You are a kid!")
elif age <= 20:
    print("You are a teenager!")
elif age <= 65:
    print("You are an adult!")
else:
    print("You are an elder!")
"""
Exercise 5-7:
喜歡的水果

"""

favorite_fruit = ['apple', 'pear', 'pitaya', 'peach']

if 'apple' in favorite_fruit:
    print("You really like apple!")
if 'pear' in favorite_fruit:
    print("You really like pear!")
if 'pitaya' in favorite_fruit:
    print("You really like pitaya!")
if 'peach' in favorite_fruit:
    print("You really like peach!")
"""
Exercise 5-8:
以特殊的方式跟管理員打招呼

"""

names = ['admin', 'b', 'c', 'd', 'e']
for i in names:
    if i == 'admin':
        print("Hello %s, would you like to see a status report?" %i)
    else:
        print("Hello %s, thank you for logging in again." %i)
"""
Exercise 5-9:
處理沒有使用者的情形

"""

names = ['admin', 'b', 'c', 'd', 'e']
del names[:]

if names:
    for i in names:
        if i == 'admin':
            print("Hello %s, would you like to see a status report?" %i)
        else:
            print("Hello %s, thank you for logging in again." %i)
else:
    print("We need to find some users!")
"""
Exercise 5-10:
檢查使用者名稱

"""

current_users = ['admin', 'B', 'c', 'd', 'e']
new_users = ['Admin', 'n', 'i', 'b', 'c']

for name in new_users:
    if name.lower() not in current_users:
        print("This name %s have not been used." %name)
    else:
        print("This name %s exist." %name)
"""
Exercise 5-11:
序數

"""

lists = [num for num in range(1,10)]
for list in lists:
    if list == 1:
        print("%d -> %dst" %(list, list))
    elif list == 2:
        print("%d -> %dnd" % (list, list))
    elif list == 3:
        print("%d -> %drd" % (list, list))
    else:
        print("%d -> %dth" % (list, list))