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

相关推荐
huisheng_qaq2 小时前
【Python基础篇-04】深入理解python的函数、参数与作用域
python·ai·函数·参数作用域
宸津-代码粉碎机6 小时前
OpenAI 连夜迎战 Grok Bot 和 Muse:AI 智能体从 “会聊天” 到 “能办事”,现在入场还来得及吗
java·大数据·人工智能·分布式·python
Y3815326626 小时前
SERP API 计费口径:credits_charged、执行失败扣费与退款边界
python·搜索引擎
chenbingjie_c7 小时前
C++ 进阶核心知识点总结:模板、内存分区、new/delete、内存池
开发语言·c++
Escalating_xu8 小时前
【C 语言】深入理解指针(1·下):指针运算、野指针、assert 与传址实战
java·c语言·开发语言
蒸鱼Yuzheng8 小时前
设备端性能工件可靠导出:断点续传、哈希、manifest 与失败恢复
android·自动化测试·python·adb·数据完整性
实心儿儿8 小时前
Qt — Qt 事件
开发语言·qt
周杰偷奶茶8 小时前
【Java】数据类型与变量
java·开发语言
code斗8 小时前
Java数据结构:堆详解
java·开发语言·数据结构
重生之小比特9 小时前
【C++进阶】map和set
java·开发语言·c++