python 第一次作业


因为笔者有一些 c/c++ 语言的基础,所以应该学 python 会稍微简单一些

格式化输出的时候,保留2位小数的格式是

python 复制代码
# 假设输出 a ,并且 a 保留 2 位小数
print('%.2f'%a)

输入

输入的时候所有的输入都是字符串类型,我们需要进行类型转换

python 复制代码
# 表示的意思是输入一个整数类型的变量 a
a=int(input())

第一题

python 复制代码
ans=4.22*3600*30/72
ans=int(ans)
print(ans)

ans2=1000000000/365/24/3600
print('%.3f'%ans2)

ans3=35.27*1.15/3
print('%.2f'%ans3)

s=12.5*16.7
c=(12.5+16.7)*2
print("面积=",'%.2f'%s,",周长=",'%.2f'%c)

第二题

python 复制代码
# a
a=int(input())
b=int(input())
c=int(input())
d=int(input())
ans=a*b-c*d
print(ans)

# b
a=int(input())
ans=5/9*(a-32)
print('%.2f'%ans)

文件操作

参见资源里面的第三题和第四题,为了方便起见,直接把代码贴在下面

第三题

python 复制代码
# Open the file in write mode
with open("a.txt", "w") as file:
    # Write student data to the file
    for i in range(5):
        student_id = input()
        student_name = input()
        student_height = input()

        # Write student data to the file
        file.write(f"{student_id},{student_name},{student_height}\n")

# print("Student data has been written to a.txt file.")

第四题

python 复制代码
# 打开文件并读取内容
with open("a.txt", "r") as file:
    # 逐行读取并显示内容
    for line in file:
        print(line.strip())  # 去除换行符并打印每一行的内容
相关推荐
我送炭你添花1 分钟前
Pelco KBD300A 模拟器:07+1.宏脚本编辑器与模板库管理实现细节
python·自动化·编辑器·运维开发
Wpa.wk6 分钟前
接口自动化测试 - REST-assure小练习(Java版-分层)
java·开发语言·经验分享·python·自动化·接口自动化·po
张人玉7 分钟前
C#WPF页面布局及其属性
开发语言·c#·wpf
_OP_CHEN8 分钟前
【从零开始的Qt开发指南】(十七)Qt 事件详解:按键与鼠标事件的全方位实战指南
开发语言·c++·qt·前端开发·qt事件·客户端开发·gui开发
qq_12498707538 分钟前
基于Java的游泳馆管理系统(源码+论文+部署+安装)
java·开发语言·毕业设计·springboot·计算机毕业设计
灵活用工平台8 分钟前
灵活用工实操案例:亲测流程与复盘分享
大数据·人工智能·python·安全
superman超哥8 分钟前
Rust 多重借用的冲突解决方案:突破借用检查的实践策略
开发语言·后端·rust·多重借用·冲突解决方案·借用检查
半夏知半秋9 分钟前
rust学习-生命周期
开发语言·笔记·后端·学习·rust
superman超哥9 分钟前
Rust 高阶 Trait 边界(HRTB)中的生命周期:超越具体生命周期的抽象
开发语言·后端·rust·生命周期·rust生命周期·rust高阶trait边界·hrtb
轻竹办公PPT10 分钟前
工作计划 PPT 使用 AI 生成时,结构清晰度如何保障
人工智能·python·powerpoint