【问题标题】:Looping and saving multiple inputs循环和保存多个输入
【发布时间】: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


【解决方案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】:

    看看您能否将这个简化的示例与您的代码联系起来。要获得您描述的输出,您需要以某种方式存储输出文本并在以后访问它:

    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)
    

    来自 cmets,仅使用字符串“accumulation”(警告:二次差):

    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) 或按上述方式逐一打印。

    【讨论】:

    • 嘿 YXD,谢谢 :)。我确实理解,但问题是它是针对学校实践问题的,并且不允许使用列表/数组/字典,并且只有在有意义的情况下才可以使用字符串累积?
    • 这是一个非常愚蠢的要求 IMO,但是您可以在循环内打印,这意味着您的输出看起来会有些不同(尝试一下),或者在循环外声明您的变量并附加到它。这在性能方面非常糟糕 - 请参阅 stackoverflow.com/a/1967732/553404 和其他人。
    • 我完全同意。显然有很多比这更简单的方法。我现在就试试看,谢谢。
    • 效果很好。再次感谢我的朋友。我比你知道的更感激它。
    【解决方案3】:

    使用输出字符串output_string

    将每一行添加到输出字符串

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

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

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

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

    【讨论】:

    • 这成功了!非常感谢我花了这么多时间在这个哈哈。我真的很感激它
    猜你喜欢
    • 2013-11-16
    • 2018-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多