用Python完成猜对一个灯谜就加10分,猜错则结束猜灯谜,并保持最好的结果、当前结果以及每次结束游戏的时候间并自定义一个函数输出最好结果、当前时间以及每次结束游戏的时间的程序的程序
时间: 2024-03-01 08:50:40 浏览: 36
好的,我会尽力回答你的问题。以下是一个简单的 Python 程序,可以实现你的需求:
```python
import random
import datetime
best_score = 0
current_score = 0
def get_best_score():
global best_score
return best_score
def get_current_score():
global current_score
return current_score
def update_score(score):
global best_score, current_score
current_score = score
if score > best_score:
best_score = score
def play_game():
global current_score
score = 0
while True:
riddle = random.choice(["什么东西一吃就死?", "什么东西一喝就死?", "什么东西一看就死?"])
answer = input(riddle)
if answer == "药" or answer == "毒药":
score += 10
print("恭喜你,回答正确!当前得分:", score)
else:
print("很遗憾,回答错误!最终得分:", score)
update_score(score)
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print("当前时间:", current_time)
break
if __name__ == "__main__":
play_game()
```
这个程序会随机选择一个灯谜,然后让玩家输入答案。如果回答正确,则加 10 分;如果回答错误,则游戏结束,并且记录最好得分、当前得分和结束时间。你可以通过调用 `get_best_score()` 和 `get_current_score()` 函数来获取最好得分和当前得分。
注意,这个程序只是一个简单的示例,你可以根据自己的需求进行修改和扩展。同时,为了保证代码的可读性和可维护性,建议你在编写代码时遵循良好的编码规范,例如使用有意义的变量名、添加注释等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)