【头歌-Python】Python初级教程第一章作业
禁止转载,原文:https://blog.csdn.net/qq_45801887/article/details/137069102
参考教程:B站视频讲解——https://space.bilibili.com/3546616042621301
Python第一章作业(初级)
第1关:浮点数四则运算与格式化输出
任务描述
Python 可以方便的实现计算器的功能。数学意义上的加、减、乘、除在Python中分别以符号“+、-、*、/”表示。
试编程实现分两行输入两个非零浮点数,并在4 行中按顺序输出两个数的加、减、乘、除的计算式和计算结果。计算结果str.format()方法严格保留小数点后3位数字。要求输出与如下示例格式相同,符号前后各有一个空格。
浮点数1 + 浮点数2 = 和
浮点数1 – 浮点数2 = 差
浮点数1 * 浮点数2 = 积
浮点数1 / 浮点数2 = 商
示例
2.66
3.1415926
2.66 + 3.1415926 = 5.802
2.66 - 3.1415926 = -0.482
2.66 * 3.1415926 = 8.357
2.66 / 3.1415926 = 0.847
参考代码
# https://blog.csdn.net/qq_45801887/article/details/137069102
a = float(input())
b = float(input())
print('{} + {} = {:.3f}'.format(a, b, a + b))
print('{} - {} = {:.3f}'.format(a, b, a - b))
print('{} * {} = {:.3f}'.format(a, b, a * b))
print('{} / {} = {:.3f}'.format(a, b, a / b))
第2关:计算矩形面积
任务描述
用户输入矩形的长和宽,计算其面积并输出。
a = eval(input()) #输入整数字符串,转换为整数;输入浮点数字符串,转换为浮点数
b = float(input()) #输入整数和浮点数,都转换为浮点数
c = int(input()) #只接受整数输入,输出为整数
#eval()和float()函数都可以把input() 函数接收到的数值型字符串转为数值类型
#例:float('3.14') 的结果是数值 3.14
#int()函数可以把整数字符串转为整数
#例:int('5') 的结果是数值 5
输入格式
第一行输入一个数字,代表矩形的长
第二行输入一个数字,代表矩形的宽
(输入使用input(),不要增加额外的提示信息)
输出格式
输出矩形的面积,不限定小数位数
示例1
3.1415926
5.88
18.472564488
示例2
5
8
40
参考代码
# https://blog.csdn.net/qq_45801887/article/details/137069102
a = eval(input())
b = eval(input())
c = a * b
print(c)
第3关:简单数学运算
任务描述
输入两个整数
a, b,顺序输出加、减和乘法运算的结果。
输入格式
第一行输入整数
a
第二行输入整数
b
输出格式
第一行输出a + b的结果
第二行输出a – b的结果
第三行输出a * b的结果
参考下面程序
def hello(user):
# 在此处输入你的代码
print('hello', user) # 你输入的多行代码都要与上一行处于相同的缩进量
print('你好!', user)
if __name__ == '__main__':
user_name = input() # 输入一个姓名
hello(user_name) # 调用你定义的函数进行输出
# 输入
# 张三
# 输出
# hello 张三
# 你好! 张三
示例
3
2
5
1
6
参考代码
# https://blog.csdn.net/qq_45801887/article/details/137069102
def solve(a,b):
# 在此处输入你的代码
print(a + b)
print(a - b)
print(a * b)
if __name__ == '__main__':
a = int(input() ) # 输入转为整数
b = int(input() ) # 输入转为整数
solve(a,b) # 调用你定义的函数进行数学运算
第4关:空格分隔格式化输出
任务描述
本关任务:编写一个日期格式化输出的小程序。
相关知识
为了完成本关任务,你需要掌握:
- Python 的空格分隔格式化输出,
格式化输出
Python 中空格分隔格式化输出:
比如要求用户输入用户名、年龄和工作,然后打印如下格式:
name = input( )
age = input( )
job = input( )
print(name, age ,job)
Danny
22
worker
Danny 22 worker
编程要求
根据提示,在右侧编辑器补充代码,在三行中分别输入当前的年、月、日的整数值,按要求完成输出。
任务:输出年月日,空格分隔,格式:2020 09 16
测试说明
平台会对你编写的代码进行测试:
2021
04
26
2021 04 26
开始你的任务吧,祝你成功!
参考代码
# https://blog.csdn.net/qq_45801887/article/details/137069102
year = input() # 输入当前年
month = input() # 输入当前月
date = input() # 输入当前日
print(year, month, date, sep=' ')
第5关:多对象的分隔符号格式化输出
任务描述
本关任务:编写一个日期格式化输出的小程序。
相关知识
为了完成本关任务,你需要掌握:
- python的多对象分隔符号输出;
格式化输出
Python中多对象分隔符号输出:
比如要求用户输入用户名、年龄和工作,然后打印如下格式:
name = input( )
age = input( )
job = input( )
print(name, age ,job , sep='-')
print(name, age ,job , sep='/')
print(name, age ,job , sep=',')
print(name, age ,job , sep='|')
Danny
22
worker
Danny-22-worker
Danny/22/worker
Danny,22,worker
Danny|22|worker
编程要求
根据提示,在右侧编辑器补充代码,在三行中分别输入当前的年、月、日的整数值,按要求完成输出。
任务:
- 输出年-月-日,连字符“-”分隔,格式:2020-09-16
- 输出年/月/日,斜线“/”分隔,格式:2020/09/16
- 输出月,日,年,逗号“,”分隔,格式:09,16,2020
测试说明
平台会对你编写的代码进行测试:
2021
04
26
2021-04-26
2021/04/26
04,26,2021
开始你的任务吧,祝你成功!
参考代码
# https://blog.csdn.net/qq_45801887/article/details/137069102
year = input() # 输入当前年
month = input() # 输入当前月
date = input() # 输入当前日
print(year, month, date, sep='-')
print(year, month, date, sep='/')
print(month, date, year, sep=',')
第6关:format方式格式化输出
任务描述
本关任务:编写一个日期格式化输出的小程序。
相关知识
为了完成本关任务,你需要掌握:
- python 的 format 格式化输出。
格式化输出
Python 中 format() 格式化输出的方式:
利用format格式化输出
format 格式化输出,比较简单,实用,f或者F都可以哦。
示例1:
name = input("请输入您的姓名:")
QQ = input("请输入您的qq:")
phone = input("请输入您的电话:")
addr=input("请输入您的地址:")
print('姓名是{} 年龄是{}岁'.format(name,25))
print('QQ是{}'.format(QQ))
print('手机号是{}'.format(phone))
print('地址是{}'.format(addr))
姓名是Bertram 年龄是25岁
QQ是123425212
手机号是010-24184241
地址是北京
示例2:
name = 'Bertram'
age = 30
print("hello,{1},you are {0}".format(age,name))#索引是根据format后的数据进行的哦
hello,Bertram,you are 30
示例3:
name = '杰'
age =26
print("hello,{name},you are {age}.".format(age=age, name=name))
hello,杰,you are 26.
编程要求
根据提示,在右侧编辑器补充代码,在三行中分别输入当前的年、月、日的整数值,按要求完成输出。
任务:用 str.format() 格式输出,格式:2021年04月26日
测试说明
平台会对你编写的代码进行测试:
2021
04
26
2021年04月26日
开始你的任务吧,祝你成功!
参考代码
# https://blog.csdn.net/qq_45801887/article/details/137069102
year = input() # 输入当前年
month = input() # 输入当前月
date = input() # 输入当前日
print('{}年'.format(year)+'{}月'.format(month)+'{}日'.format(date))
第7关:字符串拼接方式格式化输出
任务描述
本关任务:编写一个日期格式化输出的小程序。
相关知识
为了完成本关任务,你需要掌握:
- 使用字符串拼接方式进行格式化输出。
格式化输出
Python 中字符串拼接方式格式化输出的方式:
比如要求用户输入用户名、年龄和工作,然后打印如下格式:
name = input( )
age = input( )
job = input( )
print("name="+name+","+"age= "+age+","+"job="+job)
Danny
22
worker
name=Danny,age =22,job=worker
编程要求
根据提示,在右侧编辑器补充代码,在三行中分别输入当前的年、月、日的整数值,按要求完成输出。
任务:用字符串拼接方法输出,格式:2021年04月26日
测试说明
平台会对你编写的代码进行测试:
2021
04
26
2021年04月26日
开始你的任务吧,祝你成功!
参考代码
# https://blog.csdn.net/qq_45801887/article/details/137069102
year = input() # 输入当前年
month = input() # 输入当前月
date = input() # 输入当前日
print(year+'年'+month+'月'+date+'日')
第8关:欢迎入学
任务描述
本关任务:编写一个欢迎入学的小程序。
相关知识
问题描述
开学了,你考上了武汉理工大学,校园的电子屏上显示着以下欢迎界面:
|++++++++++++++++++++++|
| |
| Welcome to WHUT |
| |
|++++++++++++++++++++++|
以下为代码区:
print('|++++++++++++++++++++++|')
print('| |')
print('| Welcome to WHUT |')
print('| |')
print('|++++++++++++++++++++++|')
编程要求
根据提示,在右侧编辑器补充代码,完成相关任务的编程。
编写程序,用户入自己的姓名,输出以上界面后,再在下一行输出“欢迎您,***同学!”
测试说明
平台会对你编写的代码进行测试:
李明
;|++++++++++++++++++++++|
| |
| Welcome to WHUT |
| |
|++++++++++++++++++++++|
欢迎您,李明同学!
开始你的任务吧,祝你成功!
参考代码
# https://blog.csdn.net/qq_45801887/article/details/137069102
my_name = input() # 输入学生的姓名
########### Begin ############
# 输出以上界面
print('|++++++++++++++++++++++|')
print('| |')
print('| Welcome to WHUT |')
print('| |')
print('|++++++++++++++++++++++|')
print('欢迎您,'+my_name+'同学!')
# 输出“欢迎您,***同学!”
########### End ############
作者:谛凌