考点:
随机数库:random
时间函数库:time 注意与日期函数库(datetime)区分
代码实现
import random
import timedef display_holes(hole_index):holes = ['*' if i != hole_index else 'o' for i in range(1, 11)]print(''.join(holes))def main():total_score = 0print("欢迎来到打地鼠游戏!")for round_num in range(1, 11):print(f"第 {round_num} 轮游戏开始!")hole_index = random.randint(1, 10)display_holes(hole_index)start_time = time.time()try:hit_index = int(input("请输入你要击打的位置(1-10):"))end_time = time.time()if end_time - start_time > 10:print("超时!")continueif hit_index == hole_index:print("击中地鼠!")total_score += 1else:print("未击中地鼠!")except ValueError:print("输入无效!")print(f"游戏结束!你的总分为:{total_score}")if __name__ == "__main__":main()