简体   繁体   English

循环并保存多个输入

[英]Looping and saving multiple inputs

I'm trying to loop through a set of inputs where I ask for a user's course grade, course hours and course code. 我试图通过一组输入来询问用户的课程等级,课程时间和课程代码。 The loop keeps on repeating until the user enters "done". 循环不断重复,直到用户输入“完成”为止。 Once the user has entered done I want it to print out the entered courses with grade and hours. 用户输入完成后,我希望它打印出输入的课程以及年级和学时。

For Example: 例如:

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 ) + "%")

The problem is it will always print out only one inputted course, hour and grade. 问题在于它将始终只打印出一个输入的课程,小时和年级。 How would I save more than one answer using only accumulative strings? 仅使用累积字符串如何保存多个答案?

The output I'm looking to make is: 我想要的输出是:

# 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%

It's for a school practice problem and were not allowed to use lists, arrays or dictionaries if that makes sense 这是针对学校实践的问题,如果有意义,则不允许使用列表,数组或字典

See if you can relate this simplified example to your code. 看看是否可以将此简化示例与您的代码相关联。 To get the output you describe, you need to store the output text somehow and access it later: 为了获得您描述的输出,您需要以某种方式存储输出文本并在以后访问它:

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)

From the comments, using only string "accumulation" (warning: quadratically bad): 从注释中,仅使用字符串“ accumulation”(警告:二次错误):

output_text

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

Note that the preferred way to build up a long string is to append to a list and use 'separator'.join(list_of_strings) or print one-by-one as above. 请注意,构建长字符串的首选方法追加到列表并使用'separator'.join(list_of_strings)或如上所述逐一打印。

You may find it useful to keep your information in a dictionary structure where the key is stored as the course code. 您可能会发现将信息保存在dictionary结构中(将密钥存储为课程代码)很有用。 Then it is as simple as iterating over each course saved in your dictionary to get the details. 然后,只需遍历字典中保存的每个课程以获取详细信息,就很简单。

Example: 例:

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 ) + "%")

Use an output string output_string 使用输出字符串output_string

Add each new line to the output string 将每行添加到输出字符串

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

This accumulates the information into a string, using standard string formatting to insert the data from each pass through the loop. 这会使用标准字符串格式将信息累积到一个字符串中,以插入每次循环中的数据。

At the end of the program, print the output string. 在程序末尾,输出输出字符串。

As others have noted, this is a pretty silly way of storing data, since accessing it, except to print out, will be difficult. 正如其他人所指出的那样,这是一种非常愚蠢的数据存储方式,因为除打印输出外,访问数据非常困难。 Lists/dictionaries would be a lot better. 列表/字典会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM