題目: 設(shè)計“跳一跳”游戲的計分函數(shù),“跳一跳”游戲中黑色小人從一個方塊跳到另一個方塊上會獲得1分,
如果跳到方塊的中心點上會獲得2分,連續(xù)跳到中心點會依次獲得2分、4分、6分、……。該函數(shù)傳入一個列表,
列表中用布爾值True或False表示是否跳到方塊的中心點,函數(shù)返回最后獲得的分數(shù)
def calc_score(jump_list): total = 0 prev_on_center = False on_center_point = 2 for val in jump_list: if val: total += on_center_point on_center_point += 2 prev_on_center = True else: total += 1 on_center_point = 2 prev_on_center = False return total def main(): //測試 list1 = [True, False, False, True, True, True] list2 = [True, True, True, True, False, True, True] list3 = [False, False, True, True, True, True, True, False] print(calc_score(list1)) # 16 print(calc_score(list2)) # 27 print(calc_score(list3)) # 33 if __name__ == '__main__': main()
相關(guān)教程:Python視頻教程
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com