Home > Back-end >  Ask for Student name and grade until done is inputted?
Ask for Student name and grade until done is inputted?

Time:04-15

I am trying to write a program that stores the names/numbers the user enters in a list and uses functions to compute the maximum, minimum, and average of the students without asking how many there are. However, I don't know where to start. '''

print("-----Program for printing student name with marks using list-----")

D = {}

n = int(input('How many student record you want to store?? '))

ls = []

for i in range(0, n):


x,y = input("Enter the student name and Grade: ").split()


ls.append((y,x))


ls = sorted(ls, reverse = True)

print('Sorted list of students according to their marks in descending order')

for i in ls:


print(i[1], i[0])

CodePudding user response:

You need to use a while loop, as suggested above, and you need to grab the input as a single string before splitting, so you can check for the "done" signal.

print("-----Program for printing student name with marks using list-----")
ls = []
while True:
    s = input("Enter the student name and Grade: ")
    if s == "done":
        break
    x,y = s.split()
    ls.append((y,x))

ls.sort(reverse = True)
print('Sorted list of students according to their marks in descending order')
for i in ls:
    print(i[1], i[0])

CodePudding user response:

As mentioned in my comment, use a while loop like so:

print("-----Program for printing student name with marks using list-----")

D = {} # Not sure what this is for

ls = []

while True:
    user_input = input("Enter the student name and Grade: (done if complete)")

    # Break and exit the loop if done
    if user_input.lower() == 'done':
        break

    # Otherwise, continue adding to list
    x,y = user_input.split()
    ls.append((y,x))

ls = sorted(ls, reverse = True)

print('Sorted list of students according to their marks in descending order')

for i in ls:
    print(i[1], i[0])

CodePudding user response:

Create infinite loop with while(true): and inside the while block use if(x == "done"): break;. That will ask student name until "done" is inputted.

  • Related