# Python study diary – KillAliens realizes continuous reading and writing and computing

## foreword

It has been half a year since I put down Python. After the mid-term exam, the preliminary algorithm will be involved soon. I think that through the thinking of Python, it is very helpful to the learning of the algorithm, so I spent a few hours writing this KillAliens game.

The two most important parts are the continuous reading and writing of scores and the generation of random events. Next, I will describe in detail how to implement the entire KillAliens game and the implementation process of these two parts.

## based on

This KillAilens small game is inspired by the simple killing alien.py I wrote a long time ago. This simple text expression game has less than 30 lines. In KillAilens, the scoring system is not available in killing aliens.py , while using a dictionary, using key-value pairs to achieve each type of Ailen corresponding to its own score.

Kill Alien.py can be found in my Python learning repository.

## Code preview

############################ ### Date 2022 May 14 ### ### Author Magneto ### ### Name KillAliens <——> ### Facility Windows 11 ### ### Language Python ### ############################ import random aliens_name_and_mark = { 'Big Alien': '10', 'Middle Alien': '5', 'A Alien': '2', 'Small Alien': '1', } state_one = ['1', '2', '3', '4', '5'] The_mark = ['0'] print("欢迎来到KillAliens 小游戏，字典已经初始化完成\n接下来请输入内容\n") Your_name = input("你的名字：") print(f"你好{Your_name} ，接下来让我为你介绍游戏中有的Alien 以及其对应的分数！") for name, marks in aliens_name_and_mark.items(): print(f"\t击杀{name} 可以获得{marks} 分") while True: exit_the_game = input("如果你想退出游戏，请输入'q' 键，退出游戏，或输入其它任意值开始(继续)游戏。") if exit_the_game == 'q': break print(f"当前你的分数是{The_mark[-1]} 分") print("请输入对应的分数以选择你要击杀的Alien 击杀成功后，可获得指定分数") for name_2, makrs_2 in aliens_name_and_mark.items(): print(f"\t名称：{name_2}\n\t\t对应分数：{makrs_2}") print("请注意，你有概率无法击杀Alien 并且会因此结束游戏，并且对应分数越高，击杀几率越小。") ChoiseAliens = input("请输入对应分数：") if ChoiseAliens == '1': state_two = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] The_Random = random.choice(state_two) print("你选择了Small Alien") if The_Random in state_one: print(f"恭喜你{Your_name} ，击杀成功，获得1 分") value = The_mark[-1] float_value = float(value) end_value = float_value + 1 The_mark.append(end_value) print(f"当前总分：{The_mark[-1]} 分\n\n\n") del The_mark[-2] else: print(f"很遗憾{Your_name} ,你尝试击杀Small Alien 失败了，分数清零，退出游戏。") break if ChoiseAliens == '2': state_two = ['1', '2', '3', '6', '7', '8'] The_Random = random.choice(state_two) print("你选择了A Alien") if The_Random in state_one: print(f"恭喜你{Your_name} ，击杀成功，获得2 分") value = The_mark[-1] float_value = float(value) end_value = float_value + 2 The_mark.append(end_value) print(f"当前总分：{The_mark[-1]} 分\n\n\n") del The_mark[-2] else: print(f"很遗憾{Your_name} ,你尝试击杀A Alien 失败了，分数清零，退出游戏。") break if ChoiseAliens == '5': state_two = ['1', '2', '6', '7', '8'] The_Random = random.choice(state_two) print("你选择了Middle Alien") if The_Random in state_one: print(f"恭喜你{Your_name} ，击杀成功，获得5 分") value = The_mark[-1] float_value = float(value) end_value = float_value + 5 The_mark.append(end_value) print(f"当前总分：{The_mark[-1]} 分\n\n\n") del The_mark[-2] else: print(f"很遗憾{Your_name} ,你尝试击杀Middle Alien 失败了，分数清零，退出游戏。") break if ChoiseAliens == '10': state_two = ['1', '6', '7', '8'] The_Random = random.choice(state_two) print("你选择了Big Alien") if The_Random in state_one: print(f"恭喜你{Your_name} ，击杀成功，获得10 分") value = The_mark[-1] float_value = float(value) end_value = float_value + 10 The_mark.append(end_value) print(f"当前总分：{The_mark[-1]} 分\n\n\n") del The_mark[-2] else: print(f"很遗憾{Your_name} ,你尝试击杀Big Alien 失败了，分数清零，退出游戏。") break

To be continued