python高级练习题库实验2(B)部分


题目1

注册课程小游戏程序
研究下面的例子,并编写一个与这些例子完全相同的程序。使用for loop和break来解决问题。提示用户输入课程数量,是否选择,并且课程代码,最后还需显示已经完成的课程注册数量或者未完成的注册数量,如下如所示:
在这里插入图片描述
在这里插入图片描述

代码

num_subjects = int(input("How many subjects would you choose for this session: "))
enrolled_subjects = 0

while enrolled_subjects < num_subjects:
    continue_enrollment = input("Would you like to continue subject enrolment? (Y/N): ")

    if continue_enrollment.upper() == "Y":
        subject = input("Which subject would you like: ")
        print("You have successfully enrolled in", subject + ".")
        enrolled_subjects += 1
    else:
        break

remaining_subjects = num_subjects - enrolled_subjects

if remaining_subjects == 0:
    print("You have finished the enrollment of all", num_subjects, "subjects.")
else:
    if remaining_subjects == 1 :
        print("You have not completely finished the enrollment. There is", remaining_subjects, "subject to be enrolled.")
    else:
        print("You have not completely finished the enrollment. There are", remaining_subjects, "subjects to be enrolled.")

实验结果

在这里插入图片描述

题目2

记账小程序
研究下面的例子,并编写一个与这些例子完全相同的程序。货币在小数点后显示两位数字。提示用户输入一些基本信息,,并完成折扣与优惠卷的使用,如下图所示:
在这里插入图片描述
在这里插入图片描述
这题需要注意的是输出格式,简单的计算,提示:使用if-else语句

代码

CF = float(input("Enter the cost of Fruit&Veg: "))
CD = float(input("Enter the cost of Deli&Chilled Meals: "))
CP = float(input("Enter the cost of Pantry: "))
disc = input("Would you like to use your 10% Woolworths Mobile discount? Y or N: ")
Reword = input("Would you like to use your $10 Everyday Rewards Dollars? Y or N: ")

CFC = str(CF)
CDC = str(CD)
CPC = str(CP)

if disc.upper() == 'Y' and Reword.upper() == 'Y':
    Total = CF + CD + CP
    CT = str(Total)

    w_discount = (CF + CD + CP) * 0.1
    total_cost = Total - w_discount
    s = total_cost - 10
    print("\nReceipt:")
    print("Fruit&Veg {:>45.2f}".format(CF))
    print("Deli&Chilled Meals {:>36.2f}".format(CD))
    print("Pantry {:>48.2f}".format(CP))
    print("Total {:>49s}".format("${:.2f}".format(Total)))

    print("\n${:.2f} saved with your Woolworths Mobile discount".format(w_discount))
    print("Promotional Price {:>37s}".format("${:.2f}".format(total_cost)))

    print("\n$10 Everyday Rewards Dollars enjoyed")
    print("\nPayment {:>47s}".format("${:.2f}".format(s)))

elif disc.upper() == 'Y' and Reword.upper() == 'N':
    Total = CF + CD + CP
    CT = str(Total)
    s = Total * 0.1
    w_discount = Total * 0.1
    total_cost = Total - w_discount
    print("\nReceipt:")
    print("Fruit&Veg {:>45.2f}".format(CF))
    print("Deli&Chilled Meals {:>36.2f}".format(CD))
    print("Pantry {:>48.2f}".format(CP))
    print("Total {:>49s}".format("${:.2f}".format(Total)))

    print("\n${:.2f} saved with your Woolworths Mobile discount".format(w_discount))
    print("Promotional Price {:>37s}".format("${:.2f}".format(total_cost)))
    print("\nPayment {:>47s}".format("${:.2f}".format(total_cost)))


elif disc.upper() == 'N' and Reword.upper() == 'N':
    Total = CF + CD + CP
    CT = str(Total)
    s = Total
    print("\nReceipt:")
    print("Fruit&Veg {:>45.2f}".format(CF))
    print("Deli&Chilled Meals {:>36.2f}".format(CD))
    print("Pantry {:>48.2f}".format(CP))
    print("Total {:>49s}".format("${:.2f}".format(Total)))

    print("\nPayment {:>47s}".format("${:.2f}".format(s)))


elif disc.upper() == 'N' and Reword.upper() == 'Y':
    Total = CF + CD + CP
    CT = str(Total)
    s = Total - 10
    print("\nReceipt:")
    print("Fruit&Veg {:>45.2f}".format(CF))
    print("Deli&Chilled Meals {:>36.2f}".format(CD))
    print("Pantry {:>48.2f}".format(CP))
    print("Total {:>49s}".format("${:.2f}".format(Total)))

    print("\n$10 Everyday Rewards Dollars enjoyed")
    print("\nPayment {:>47s}".format("${:.2f}".format(s)))
else:
    print("")

实验结果

在这里插入图片描述

题目总结

题目1:注册课程小游戏程序
研究下面的例子,并编写一个与这些例子完全相同的程序。使用for loop和break来解决问题。提示用户输入课程数量,是否选择,并且课程代码,最后还需显示已经完成的课程注册数量或者未完成的注册数量

题目2:记账小程序
研究下面的例子,并编写一个与这些例子完全相同的程序。货币在小数点后显示两位数字。提示用户输入一些基本信息,,并完成折扣与优惠卷的使用

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI敲代码的手套

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

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

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

打赏作者

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

抵扣说明:

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

余额充值