Brandon Gregoire Brandon Gregoire - 1 month ago 10
Python Question

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

Answer

Use an 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.