python基础练习题库实验2

题目1

编写一个程序,要求用户输入产品代码、产品名称、产品尺寸和产品价格。

然后使用字符串格式来显示产品信息,就像下面的示例一样。

请注意,价格必须使用两位十进制数字显示。

代码

python 复制代码
product_code = input("Enter product code: ")
product_name = input("Enter product name: ")
product_size = input("Enter product size: ")
product_price = input("Enter product price: ")

product_code_s = str(product_code)
product_name_s = str(product_name)
product_size_s = str(product_size)

number = float(product_price)
print(product_code_s + ": " + product_name_s + ", " + product_size_s + "\n" +
      product_name_s + ", " + product_size_s + ", " + "$%.02f" % number + "\n" + product_code_s + ": " + "\"" + product_name_s + "\"")

实验结果

题目2

编写一个使用字符串格式的程序,以产生以下精确的输出。

例如:

代码

python 复制代码
for i in range(1,4):
    s=0
    s=i*i
    a=str(i)
    re=str(s)
    print(" "+a+" x "+" "+a+" =  "+" "+re)
    
for i in range(4,10):
    s=0
    s=i*i
    a=str(i)
    re=str(s)
    print(" "+a+" x "+" "+a+" =  "+re)

for i in range(10,11):
    s=0
    s=i*i
    a=str(i)
    re=str(s)
    print(a+" x "+a+" = "+re)

实验结果

这个题目主要是对齐

题目3

--输入/输出--

使用字符串格式编写一个程序,该程序产生以下输出,其中

-使用右对齐和10个空格的第一列

-使用中心对齐和30个空格的第二列

-第3列使用左对齐和10个空格

代码

python 复制代码
print("{0:>10}{1:^30}{2:<10}".format("Faces","Name","Vertices"))
print("{0:>10}{1:^30}{2:<10}".format("4","Tetrahedron","4"))
print("{0:>10}{1:^30}{2:<10}".format("6","Cube","8"))
print("{0:>10}{1:^30}{2:<10}".format("8","Octahedron","6"))
print("{0:>10}{1:^30}{2:<10}".format("12","Dodecahedron","20"))
print("{0:>10}{1:^30}{2:<10}".format("20","Icosahedron","12"))

实验结果

题目总结

🎈这次实验主要考察python的输出的对齐,格式,小数点的语法,如果还不太熟练的小伙伴可以去查看我的python基础系统

相关推荐
伊灵eLing8 分钟前
GoLang 语言基础
开发语言·后端·golang
两年半的个人练习生^_^8 分钟前
JMM 进阶:彻底理解 synchronized 实现原理
java·开发语言
小白不白11110 分钟前
Invoke的用法
开发语言·人工智能·数码相机·计算机视觉·c#
c_lb728810 分钟前
期货量化策略从 Windows 迁到 Linux 服务器:环境注意点
linux·服务器·windows·python
techdashen13 分钟前
What is maintenance, anyway?
开发语言·后端·rust
北凉军15 分钟前
更换PyCharm 任务栏图标
ide·python·pycharm
万法若空16 分钟前
C/C++基本类型表示范围
c语言·开发语言·c++
yijianace19 分钟前
Python爬虫实战:BooksToScrape 多线程爬取与图片下载
开发语言·爬虫·python
LadenKiller19 分钟前
期货多品种轮动标的池:天勤 query_quotes 筛品种写法
python·区块链
郑洁文20 分钟前
基于Python+回归分析的电子产品需求数据分析与预测
python·数据分析·回归·电子产品需求数据·电子产品数据分析