循环并保存多个输入

时间:2016-10-16 20:04:36

标签: python

我试图在一组输入中循环,我要求用户的课程成绩,课程时间和课程代码。循环不断重复,直到用户输入"完成"。一旦用户输入完成,我希望它打印输入的等级和小时的课程。

例如:

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%

它是针对学校练习的问题,如果有意义的话,不允许使用列表,数组或词典

3 个答案:

答案 0 :(得分:1)

看看您是否可以将此简化示例与您的代码相关联。要获得您描述的输出,您需要以某种方式存储输出文本并稍后访问它:

output_lines = []

for i in range(10):
  input_string = input("Enter some input")
  output_lines.append(input_string)

for output_line in output_lines:
  print(output_line)

从评论中,仅使用字符串“accum”(警告:二次方错):

output_text

for i in range(10):
  input_string = input("Enter some input")
  output_text = output_text + '\n' + input_string
print(output_text)

请注意,构建长字符串的首选方法是以附加到列表并使用'separator'.join(list_of_strings)或逐个打印。

答案 1 :(得分:1)

您可能会发现将信息保存在dictionary结构中非常有用,其中密钥存储为课程代码。然后就像迭代字典中保存的每个课程一样简单,以获取详细信息。

示例:

course_count = False
course_info = {}
#LOOP through Inputs
while not course_count:

    #GET course code
    course_code = input( "Please Enter the Course Code (or done if finished): " )
    course_info[course_code] = {};

    #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 + "? " ) )
        course_info[course_code]['hours'] = course_hours;

        #GET grade earned
        course_grade = float( input( "What grade did you earn in " + course_code + "? " ) )
        course_info[course_code]['grade'] = course_grade

    #ELSE END LOOP
    else:
        course_count = True

For course_code in course_info :
    course_hours = course_info[course_code]['hours']
    course_grade = course_info[course_code]['grade']
    print("Course: " + course_code + " Weight: " + str( course_hours ) + " hours " + "Grade: " + str( course_grade ) + "%")

答案 2 :(得分:0)

使用输出字符串output_string

将每个新行添加到输出字符串

...
output_string += "Course: {} Weight: {} hours Grade: {}\n".format(course_code, course_hours, course_grade"
#ELSE END LOOP
...

这会将信息累积到字符串中,使用标准字符串格式来插入每次循环中的数据。

在程序结束时,打印输出字符串。

正如其他人所指出的那样,这是一种非常愚蠢的存储数据的方式,因为除了打印外,访问它将很困难。列表/词典会好很多。