Skip to content

Instantly share code, notes, and snippets.

@liuderchi liuderchi/wk2-answers.py
Last active Dec 1, 2015

Embed
What would you like to do?
'''題目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
You can’t perform that action at this time.