0

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])
Throwaway
  • 11
  • 1
  • 1
    you need to use a while loop instead of a for loop at `for i in range(0, n)`, and then check if the input is done. If so, you can exit the loop with the `break` keyword. Otherwise, add it to your list – rcshon Apr 14 '22 at 19:26
  • This actually looks like a pretty good start, except that the indentation is messed up so I can't run it to see what it does. What problem are you having? – Samwise Apr 14 '22 at 19:27
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Apr 14 '22 at 19:42

3 Answers3

0

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])
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

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])
rcshon
  • 907
  • 1
  • 7
  • 12
0

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.

Dragesia
  • 25
  • 6