python编程复习系列——week3决策声明(if-else)


一、理解决策制定陈述的使用情况

多选语句if-elif-else是最常见的决策语句。

我们使用这个控制结构来指定程序中的两个不同操作。

我们需要提供一个条件。如果这个条件成立,那么我们的程序将执行某个操作。如果这个条件为假,那么将采取另一个操作。
示例:

if (language_option == "I"):
#{
 print("Ciao mondo.")
 print("Come stai?")
#}
elif (language_option == "W"):
#{
 print("Helo Byd.")
 print("Sut wyt ti?")
#}
elif (language_option == "Z"):
#{
 print("Sawubona Mhlaba.")
 print("Unjani?")
#}
else:
#{
 print("Hello World.")
 print("How are you?")
#}

二、if-elif例题

编写一个程序,根据以下定价计算成本。

项目数量成本
1-50每件3美元 ;邮费:10美元
超过50每件2美元邮寄:免费
num_items = input("Enter the number of items: ")
items = int(num_items)
num_items_str = str(num_items)

if items <= 50 and items >= 1:
    print("\nReceipt:")
    subtotal = items * 3
    subtotal_str = str(subtotal)
    print(num_items_str + " items" + " x " + "$3" + " = " + "$" + subtotal_str)
    print("Postage: $10")
    total = subtotal + 10
    total_str = str(total)
    print("Total: " + "$" + total_str)
elif items > 50:
    print("\nReceipt:")
    subtotal = items * 2
    subtotal_str = str(subtotal)
    print(num_items_str + " items" + " x " + "$2" + " = " + "$" + subtotal_str)
    print("Postage: $0")
    print("Total: " + "$" + subtotal_str)
else:
    print("Please enter the number again!")

总结

🍖if -elif-else语句在题目当中一般不会单独出现,这是一个非常重要的语句,功能也非常强大,需要大量练习,我也准备了一下练习题,供大家参考。今天就先学习到这里吧!

  • 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、付费专栏及课程。

余额充值