Today I will show you a Python written test question designed to design a "jump" mini-game scorer. It has certain reference value and is also very simple and easy to learn. Friends who are interested can learn about it.
Title: Design the scoring function of the "Jump" game. In the "Jump" game, the black villain will get 1 point by jumping from one block to another.
If he jumps The center point of the block will get 2 points. Jumping to the center point continuously will get 2 points, 4 points, 6 points, etc. This function passes in a list.
The Boolean value True or False is used in the list to indicate whether to jump to the center point of the square. The function returns the last score obtained
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()
Related tutorials:Python Video tutorial
The above is the detailed content of Python written test questions: Designing a 'jump-a-jump' mini-game scorer. For more information, please follow other related articles on the PHP Chinese website!