【发布时间】:2016-10-16 20:04:35
【问题描述】:
我正在尝试遍历一组输入,在这些输入中我询问用户的课程成绩、课程时间和课程代码。循环不断重复,直到用户输入“完成”。用户输入完成后,我希望它打印出输入的课程以及成绩和小时数。
例如:
course_count = False
#LOOP through Inputs
while not course_count:
#GET course code
course_code = input( "Please Enter the Course Code (or done if finished): " )
#IF course code is not equal to done (convert to lowercase)
if course_code.lower() != "done":
#GET course hours
course_hours = int( input( "How many credit hours was " + course_code + "? " ) )
#GET grade earned
course_grade = float( input( "What grade did you earn in " + course_code + "? " ) )
#ELSE END LOOP
else:
course_count = True
print("Course: " + course_code + " Weight: " + str( course_hours ) + " hours " + "Grade: " + str( course_grade ) + "%")
问题是它总是只打印一门输入的课程、小时数和年级。如何仅使用累积字符串保存多个答案?
我想要的输出是:
# Please Enter the Course Code (or done if finished): COMP 10001
# How many credit hours was COMP 10001? 5
# What grade did you earn in COMP 10001? 75
# Please Enter the Course Code (or done if finished): COMP 20002
# How many credit hours was COMP 10001? 8
# What grade did you earn in COMP 10001? 95
# Please Enter the Course Code (or done if finished): done
# Course: COMP 10001 Weight: 5 Grade: 75%
# Course: COMP 20002 Weight: 8 Grade: 95%
这是针对学校练习题的,如果有意义,则不允许使用列表、数组或字典
【问题讨论】:
-
打印前添加标签?
-
@tim 不会给出他想要创建的格式化输出,其中提供了运行时插入的信息的摘要。
标签: python