20212312 2023-2024-2 《Python程序设计》实验一报告

20212312应万里 2024-03-18 16:40:41

课程:《Python程序设计》
班级: 2123
姓名: 应万里
学号:20212312
实验教师:王志强
实验日期:2024年3月13日
必修/选修: 公选课

1.实验内容

1.熟悉Python开发环境;

2.练习Python运行、调试技能;

3.编写程序,练习变量和类型、字符串、对象、缩进和注释等;

  1. 掌握git技能(可把猜数字游戏上传到gitee)

2. 实验过程及结果

1. 熟悉Python开发环境

之前做大创和课外竞赛时接触到jupyter,感觉的项目的结构很直观非常方便,我一直使用jupyter作为python开发环境。

img

img

2.练习Python运行、调试技能

①直接整体运行了实验前写好的数字记忆游戏

img

运行结果符合预期,我还自己玩了一会儿,结果早早的就game over了。

②与vscode、pycharm中使用断点进行调试不同,jupyter没有提供直接可用的debug按钮,但是可以把代码拆成不同的模块,放入jupyter的各个输入框中,这样就可以间接实现断点

img

3.编写程序,练习变量和类型、字符串、对象、缩进和注释等

这是我编写的数字记忆游戏,规则如下,游戏开始时,系统先依次给出n个随机自然数字(n=2,3,4,5)每个数字只依次显示1s,游戏开始后,每个回合中,系统给出一个新的第x个随机数字,新数字显示2s,玩家需在2s内输入第x-n个数字,正确写出并回车则该回合成功并进入下一回合,否则游戏失败。游戏结束后输出玩家通过的回合数。

import os
import time
import random
import platform
import threading

def clear_screen():
    os_name = platform.system()
    if os_name == "Windows":
        os.system('cls')
    else:
        os.system('clear')

user_input = None

def get_input(prompt):
    global user_input
    user_input = input(prompt)

def memory_game():
    global user_input
    print("请选择难度级别(输入对应的数字):\n1. 简单\n2. 中等\n3. 困难\n4. 非常困难")
    difficulty = input("你的选择是:")
    difficulty_level = int(difficulty)
    
    if difficulty_level == 1:
        n = 2
    elif difficulty_level == 2:
        n = 3
    elif difficulty_level == 3:
        n = 4
    elif difficulty_level == 4:
        n = 5
    else:
        print("无效的选择,游戏将使用默认难度(简单).")
        n = 2

    numbers = [random.randint(0, 9) for _ in range(n)]
    round_passed = 0

    print("游戏开始!请记住下面的数字:")
    for number in numbers:
        print(number)
        time.sleep(1)
        clear_screen()

    try:
        while True:
            new_number = random.randint(0, 9)
            numbers.append(new_number)
            
            print(f"新的数字是:{new_number}")
            time.sleep(2)
            clear_screen()
            
            # 启动一个线程来获取用户输入
            user_input = None
            input_thread = threading.Thread(target=get_input, args=("请输入之前的数字:",))
            input_thread.start()
            input_thread.join(timeout=2)  # 等待2秒
            
            if not input_thread.is_alive():
                expected_number = numbers[-n-1]
                if user_input is not None and int(user_input) == expected_number:
                    round_passed += 1
                    print("回合成功!进入下一回合。")
                    time.sleep(1)
                    clear_screen()
                else:
                    print(f"游戏失败!你通过了{round_passed}回合。正确数字是:{expected_number}")
                    break
            else:
                print(f"输入超时!游戏失败。你通过了{round_passed}回合。")
                break
    except Exception as e:
        print(f"游戏异常结束:{e}")
    finally:
        input("按任意键退出...")

memory_game()

### 4.掌握git技能

![img](https://img-community.csdnimg.cn/images/80def51ff2444a53b9d4554747715420.png "#left")

3. 实验过程中遇到的问题和解决过程

  • 问题1:jupyter无法直接设置断点debug
  • 问题1解决方案:利用jupyter可以分模块运行的特性,将需要调试的代码装入单独的输入框中即可间接实现断点调试

其他(感悟、思考等)

编写数字记忆游戏是一个挑战,也是一个学习的机会。通过这个项目,我不仅复习和应用了Python中的基础概念,如变量、循环和条件判断,还学习了如何管理和维护代码版本使用git。

...全文
38 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110

社区成员

发帖
与我相关
我的任务
社区描述
人生苦短,我用Python!
python3.11 高校
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧