Chapter 3 條件運算與迴圈

3-1 條件運算 if

Python的條件運算共有三類:  if,  if … else,  if … elif ….elif … else

1. if 語法

ans = input('Do you want some cookies?')
if ans == "y":
print ("Yes, I want some cookies.")
print("and you?")
print("over")

ans = input('Do you want some cookies?').upper()
# ans = ans.upper()
if ans == "Y":
print ("Yes, I want some cookies.")
print("and you?")
print("over")

2. if … else 語法

 if condition:

     statement A

 else:

     statement B

a = eval(input('請輸入成績: '))
if a >= 60:
b = 'pass'
else:
b = 'fail'
print('成績', a , '分', b )