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

相关推荐
怪兽20143 小时前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin
ClearLiang3 小时前
Kotlin-协程的挂起与恢复
开发语言·kotlin
彭同学学习日志3 小时前
Kotlin Fragment 按钮跳转报错解决:Unresolved reference ‘floatingActionButton‘
android·开发语言·kotlin
海域云赵从友3 小时前
破解跨境数据传输瓶颈:中国德国高速跨境组网专线与本地化 IP 的协同策略
开发语言·php
咚咚王者4 小时前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
深蓝海拓4 小时前
使matplot显示支持中文和负号
开发语言·python
AntBlack5 小时前
AI Agent : CrewAI 简单使用 + 尝试一下股票分析
后端·python·ai编程
一眼万里*e5 小时前
搭建本地deepseek大模型
python
1***Q7845 小时前
PyTorch图像分割实战,U-Net模型训练与部署
人工智能·pytorch·python
syt_biancheng5 小时前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法