Skip to content

Instantly share code, notes, and snippets.

@liuderchi
Last active December 1, 2015 00:45
Show Gist options
  • Save liuderchi/911ad59091b8ea2ae43a to your computer and use it in GitHub Desktop.
Save liuderchi/911ad59091b8ea2ae43a to your computer and use it in GitHub Desktop.
'''題目1'''
weekdays = ['Mon','Tue','Wed','Thu','Fri']
def good_print_weekday( num, weekdays ):
if num > 5:
print 'index too large'
else:
print weekdays[ num - 1]
good_print_weekday( 2, weekdays ) #Tue
good_print_weekday( 7, weekdays ) #index too large
'''如果輸入的數字太大 會被第4行的判斷式發現以避免 index out of range 錯誤發生'''
'''題目2'''
def umbrella_check( p ):
if p > 50:
print 'remember your umbrella'
else:
print 'no need umbrella'
umbrella_check( 30 ) #no need umbrella
umbrella_check( 51 ) #remember your umbrella
'''題目2 加分題'''
def good_umbrella_check( p ):
if p > 50:
if p == 100:
print 'remember umbrella and raincoat'
else:
print 'remember your umbrella'
else:
print 'no need umbrella'
good_umbrella_check( 50 ) #no need umbrella
good_umbrella_check( 99 ) #remember your umbrella
good_umbrella_check( 100 ) #remember umbrella and raincoat
'''題目2 進階題'''
def rank( score ):
if score >= 80:
print '甲'
else:
if score >= 70:
print '乙'
else:
if score >= 60:
print '丙'
else:
print '丁'
rank(50)
rank(60)
rank(70)
rank(80)
rank(85)
'''題目3'''
def forever_hello():
i = 0
while i > -10:
print 'hello'
#forever_hello()
'''可以在while中添加 i = i - 1'''
def forever_goodbye():
i = 1
while i != 10:
print 'goodbye'
i = i + 2
#forever_goodbye()
'''可以將 i = 1 改成 i = 0'''
'''題目4'''
def print_list(schedule):
i = 0
while i < len(schedule):
print schedule[i]
i = i + 1
monday = ['國', '國', '數', '數', '體', '英', '英']
wed = ['美', '美', '自', '自']
print_list( monday )
print_list( wed )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment