python编程复习系列——week2(Input & Output (2))


一、多行代码语句

🥞使用反斜杠\来表示在下一行中继续使用一条语句。

subject_code = "CSCI111"
subject_mark = 80
subject_grade = "D"
result = "Subject result: " \
 + subject_code \
 + " mark " + str(subject_mark) \
 + " grade " + subject_grade
print(result)

🍗当语句在括号内时,行延续是自动的

subject_code = "CSCI111"
subject_mark = 80
subject_grade = "D"
print( 
 "Subject result: " 
 + subject_code 
 + " mark " + str(subject_mark) 
 + " grade " + subject_grade 
)

🍔有时,我们应该把一长串代码分解成多行代码,使它更清晰

二、Escape序列

我们如何为这个输出编写程序:
Thursday: “Inside Out”

print("Thursday: \"Inside Out\"")

转义序列含义
\:后间隙(\)
‘:单引号(’)
\“:双引号(”)
\n:新行
\t:选项卡

print("Your details:\n")
print("\tName: \"John Smith\"")
print("\tSN: \"2012345\"")
print("\nEnrolment record:\n")
print("\tMATH101")
print("\tCSCI201")

运行结果如下

Your details:
	
	Name: "John Smith"
	SN: "2012345"
 
Enrolment record:
	MATH101
	CSCI201

三、字符串格式

fname = "John"
lname = "Smith"
age = 20
gpa_score = 3.2
print("Hi {0} {1}!".format(fname, lname))
print("{1} {2} is {0} years old".format(age, fname, lname))
print("His GPA score is {0:.2f}".format(gpa_score))

在这里插入图片描述
带对齐的字符串格式
{0:<15} —— 左对齐,使用15个空格
{1:<10} —— 左对齐,使用10个空格
{2: ^25} —— 中心对齐,使用25个空间
{3:>15} —— 右对齐,使用15个空格

print("{0} x {1} = {2}".format(1, 5, 1*5))
print("{0} x {1} = {2}".format(2, 5, 2*5))
print("{0} x {1} = {2}".format(3, 5, 3*5))
print("{0} x {1} = {2}".format(4, 5, 4*5))
print("{0} x {1} = {2}".format(5, 5, 5*5))
print("{0} x {1} = {2}".format(6, 5, 6*5))
print("{0} x {1} = {2}".format(7, 5, 7*5))
print("{0} x {1} = {2}".format(8, 5, 8*5))
print("{0} x {1} = {2}".format(9, 5, 9*5))
print("{0} x {1} = {2}".format(10, 5, 10*5))

在这里插入图片描述

print("{0:>2} x {1:>1} = {2:>2}".format(1, 5, 1*5))
print("{0:>2} x {1:>1} = {2:>2}".format(2, 5, 2*5))
print("{0:>2} x {1:>1} = {2:>2}".format(3, 5, 3*5))
print("{0:>2} x {1:>1} = {2:>2}".format(4, 5, 4*5))
print("{0:>2} x {1:>1} = {2:>2}".format(5, 5, 5*5))
print("{0:>2} x {1:>1} = {2:>2}".format(6, 5, 6*5))
print("{0:>2} x {1:>1} = {2:>2}".format(7, 5, 7*5))
print("{0:>2} x {1:>1} = {2:>2}".format(8, 5, 8*5))
print("{0:>2} x {1:>1} = {2:>2}".format(9, 5, 9*5))
print("{0:>2} x {1:>1} = {2:>2}".format(10, 5, 10*5))

在这里插入图片描述

四、数值运算

算术运算符
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

课后作业

一家商店出售的产品售价为10美元,但折扣是3件商品只需要20美元。编写一个程序,要求用户输入他们想要购买的物品的数量。然后,该程序将显示成本。

item_price = 10  # 商品单价
discount_quantity = 3  # 折扣适用的商品数量
discount_price = 20  # 折扣后的价格

# 用户输入购买的物品数量
quantity = int(input("请输入您想购买的物品数量:"))

# 计算总成本
if quantity < discount_quantity:
    cost = item_price * quantity  # 不满足折扣条件,按单价计算成本
else:
    # 计算折扣后的商品数量和单价
    discounted_quantity = quantity // discount_quantity * discount_quantity
    remaining_quantity = quantity % discount_quantity
    cost = (discounted_quantity / discount_quantity * discount_price) + (remaining_quantity * item_price)

# 显示成本
print("购买", quantity, "件商品的成本为", cost, "美元。")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI敲代码的手套

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值