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