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基础系统

相关推荐
派大鑫wink14 小时前
网络安全新挑战:AI 驱动的攻防对抗与防护策略(附实战操作)
人工智能·python·安全·web安全
CCPC不拿奖不改名14 小时前
Python基础:python语言中的文件操作+面试题目
开发语言·数据结构·人工智能·python·学习·面试·职场和发展
superman超哥14 小时前
Rust 借用分割技巧:突破借用限制的精确访问
开发语言·后端·rust·编程语言·借用分割技巧·借用限制·精准访问
程序炼丹师14 小时前
C++ 中的 std::tuple (元组)的使用
开发语言·c++
海棠AI实验室14 小时前
第十八章Notebook 工作流:可复现实验与科研记录
python·notebook
程序员佳佳14 小时前
【万字硬核】从GPT-5.2到Sora2:深度解构多模态大模型的“物理直觉”与Python全栈落地指南(内含Banana2实测)
开发语言·python·gpt·chatgpt·ai作画·aigc·api
不绝19114 小时前
C#进阶——内存
开发语言·c#
风送雨14 小时前
Go 语言进阶学习:第 1 周 —— 并发编程深度掌握
开发语言·学习·golang
带娃的IT创业者14 小时前
Cursor 新增的 Plan 和 Debug 模式介绍
人工智能·python·cursor·ai辅助开发·ai开发工具·ai开发技巧
小北方城市网14 小时前
第 5 课:服务网格(Istio)实战|大规模微服务的流量与安全治理体系
大数据·开发语言·人工智能·python·安全·微服务·istio