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

相关推荐
slandarer7 分钟前
MATLAB | 泰勒图绘制,支持各种角度范围
开发语言·数学建模·matlab·nature·顶刊·泰勒图
元Y亨H16 分钟前
Pandas 解析 Excel 导致的内存溢出(MemoryError)
python·excel
mct12320 分钟前
c++ iconv 字符utf-8转换gb2312失败
开发语言·c++
金銀銅鐵1 小时前
[Python] 用 turtle 来绘制瑞典国旗
python
Albert Edison1 小时前
【GUI 自动化测试】Pywinauto 常见操作
自动化测试·python·pywinauto
海兰1 小时前
【高速缓存】RedisVL为文本生成嵌入向量实践指南
人工智能·python·机器学习
2601_955760071 小时前
如何用 Claude Opus 5 API 批量扩展长尾关键词和文章选题
前端·python·搜索引擎
艾斯特_2 小时前
从模型调用到可用聊天应用:会话状态与消息协议
python·ai·aigc
Marst Code2 小时前
Python 3.9 已停止维护!从 3.9 到 3.14 全版本深度对比,生产环境该选哪个?
开发语言·python
llwszx2 小时前
【Java/Go后端手撸原生Agent(第九篇):Plan-and-Execute规划模式——从“走一步看一步“到“先谋后动“】
java·python·golang·agent开发·plan模式·规划执行模式