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())  # 去除换行符并打印每一行的内容
相关推荐
安易算力26 分钟前
PUE优化工程实践:从1.5到1.2的制冷架构与气流组织改造路径
网络·python·容器·架构·kubernetes
troy1281 小时前
Codex 安全盲区:代码漏洞生成实测
windows·python·ci/cd·pycharm·django·github·fastapi
嵌入式学习菌1 小时前
Workbuddy自动写一个RS485 / LoRa 参数调试工具
python
钱栈up2 小时前
番茄小说榜单爬虫失效排查:从页面路由变更到API参数映射的全流程复盘
python
唐璜Taro2 小时前
Agent Harness 系列 · 第 1 篇|Agent 不只是换一个更强的模型
人工智能·python
码行山野赴时序归途2 小时前
链表家族收官篇:循环链表
c语言·开发语言·数据结构·链表
泡茶喝茶写代码2 小时前
量化数据进阶:多维实战篇(第 11 篇):历史涨跌停价:涨跌停序列与止损线测算
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据api
景熙55233 小时前
单体项目编写思路和落地形式
java·开发语言
ikun_文3 小时前
Django开启跨域请求
python·pycharm·django
外收内放3 小时前
Python与AI应用(项目开发实战:AI智能伴侣第三版)
python·学习·ai编程