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

发布时间:2024年01月18日


题目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.")

实验结果

在这里插入图片描述</

文章来源:https://blog.csdn.net/qq_43499381/article/details/135652987
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。