20222227 实验一《Python程序设计》实验报告

20222227 2024-2025-2 《Python程序设计》实验一报告
课程:《Python程序设计》
班级: 2222
姓名: 赫连紫阳
学号: 20222227
实验教师:王志强
实验日期:2025年3月12日
必修/选修: 公选课
一、实验内容
1.熟悉Python开发环境;
2.练习Python运行、调试技能;(编写书中的程序,并进行调试分析,要有过程)
3.编写程序,练习变量和类型、字符串、对象、缩进和注释等;(编写一个猜数字或者剪刀石头布的游戏)
4.掌握git技能

二、 实验过程及结果
1.熟悉Python开发环境
下载并安装python环境

下载安装PyCharm

2.练习Python运行、调试技能
运行简易计算器程序
程序如下:

    def simple_calculator():
    print("=== 简易计算器 ===")
    while True:
    try:
        # 获取第一个数字
        num1 = float(input("请输入第一个数字: "))
        
        # 获取运算符并验证
        operator = input("请输入运算符 (+ - * /): ")
        while operator not in ['+', '-', '*', '/']:
            print("无效运算符,请重新输入")
            operator = input("请输入运算符 (+ - * /): ")
        
        # 获取第二个数字
        num2 = float(input("请输入第二个数字: "))
        
        # 执行计算
        if operator == '+':
            result = num1 + num2
        elif operator == '-':
            result = num1 - num2
        elif operator == '*':
            result = num1 * num2
        elif operator == '/':
            if num2 == 0:
                print("错误:除数不能为零!")
                continue
            result = num1 / num2
        
        # 显示结果(自动去除小数部分)
        if result.is_integer():
            print(f"计算结果: {int(result)}")
        else:
            print(f"计算结果: {result:.2f}")  # 保留两位小数
        
    except ValueError:
        print("错误:请输入有效的数字!")
    except Exception as e:
        print(f"发生错误: {str(e)}")
    
    # 继续运行判断
    choice = input("\n是否继续计算?(y/n): ").lower()
    if choice != 'y':
        print("感谢使用计算器!")
        break
    print("--------------------")

运行结果如下:

调试程序

3.编写一个猜数字游戏
代码如下:

import random

def guess_game():
print("=== 猜数字游戏 ===")

# 难度设置
levels = {
    "1": {"name": "简单", "max_num": 50, "tries": 10},
    "2": {"name": "普通", "max_num": 100, "tries": 10},
    "3": {"name": "困难", "max_num": 200, "tries": 10}
}

# 选择难度
print("请选择难度:")
print("1. 简单(1-50,)")
print("2. 普通(1-100)")
print("3. 困难(1-200)")

while True:
    choice = input("输入数字选择难度 (1/2/3): ")
    if choice in levels:
        config = levels[choice]
        break
    print("输入错误,请重新选择!")

# 初始化游戏
secret = random.randint(1, config["max_num"])
remaining = config["tries"]

print(f"\n{config['name']}模式开始!数字范围1-{config['max_num']}")
print(f"你有{remaining}次机会,加油!")

# 游戏循环
for attempt in range(1, config["tries"] + 1):
    try:
        guess = int(input(f"\n第{attempt}次猜测: "))

        # 验证输入范围
        if guess < 1 or guess > config["max_num"]:
            print(f"请输入1到{config['max_num']}之间的数字!")
            continue

        # 判断结果
        if guess == secret:
            print(f"恭喜!第{attempt}次猜中了!")
            return
        elif guess < secret:
            print("猜小了!")
        else:
            print("猜大了!")

        # 显示剩余次数
        print(f"剩余机会: {config['tries'] - attempt}")

    except ValueError:
        print("请输入数字!")

print(f"\n游戏结束,正确答案是:{secret}")

运行结果:

4.掌握git技能
使用git将代码上传到gitee

在gitee仓库中查看提交的代码

我的gitee仓库连接:https://gitee.com/hlzy2004/python/blob/master/caishuzi.py

三、实验过程中遇到的问题和解决过程

一开始不了解如何将代码托管到gitee之中
解决过程:
通过查询相关资料,了解了具体过程

参考资料
https://blog.csdn.net/weixin_42242910/article/details/138845248

posted @ 2025-03-18 20:14  望舒、  阅读(14)  评论(1)    收藏  举报