How to place items in a loop on a table in alphabetical order?

Viewed 52

this is my homework:

Write a program that asks for the names and ages of 10 students. Prints the names and ages of the students alphabetically in a table format -- while also identifying the oldest student as the "Team Leader".

And this is what I've done so far. Comments are what I think I'm supposed to do but not sure:

#from tabulate import tabulate

studentNames = []
for i in range(10):
          item = input("Please enter a name: ")
          if len(item) > 0 and item.isalpha():
                 studentNames.append(item)

#Something like this? 
#print(tabulate(studentNames([[]], headers=['Team Leader', 'Students']))

My teacher didn't teach me tabulate but my research says I need to use it, but I think it's wrong and I don't know how to do it alphabetically. I'm sorry for the bad code, I'm embarrassed to even post it.

Any help would be appreciated. Thank you.

2 Answers

I don`t think it help when I solve the exercise for you. I would recommend you figure out how to add the input like the var table.

table = [("A", 20), ("Z", 21), ("C", 29), ("B", 24)]
sorted_table = sorted(table, key=lambda row: row[0])
leader = max(table, key=lambda row: row[1])

To print it you can simply iterate over the rows and print something like this:

print("{0} | {1}".format(row[0], row[1]))

My complete solution is this I would recommend you only use it if you are stuck again.

table = []
for i in range(10):
    name = input("Please enter a name: ")
    age = input("Enter the age: ")
    if len(name) > 0 and name.isalpha():
        table.append((name, age))

sorted_table = sorted(table, key=lambda row: row[0])
leader = max(table, key=lambda row: row[1])

print("{0:<12} | {1:<4}".format("Names", "Age"))
print("-" * 19)
for row in sorted_table:
    print("{0:<12} | {1:<4}".format(row[0], row[1]))

print("Team Leader is: {0}".format(leader[0]))

Right... This is 90% of the Code... Considering it's your homework, you'll only need to find a quick way to add "Oldest Student" to the end of the Oldest Student which I'm sure you'll do pretty quick. I tried making it easy to read so you can learn. Good Luck!

# Python-3 (Change it slightly to make it usable for Python-2)

# Create an empty List to store input data...
studentDataList = []

# Little Extra to Spice up the Program...
numberOfStudents = int(input("Please Enter the Number of Students you Wish to Enter Data for: "))
studentCount = 1

# Create a Function for requesting data...
def getStudentData():
    # Use variables outside this function
    global studentCount

    # Get Data and Verify
    studentNameData = input("Enter Student (" + str(studentCount) + ") Name: ")
    if len(studentNameData) != 0 and studentNameData.isalpha() == True:
        studentAgeData = int(input("Enter Student (" + str(studentCount) + ") Age: "))
        if studentAgeData != 0:
            studentDataList.append(studentNameData + " " + str(studentAgeData))
            studentCount += 1
        else:
            print("Invalid Entry")
            getStudentData()
    else:
        print("Invalid Entry")
        getStudentData()

# Run For Loop...
for num in range(numberOfStudents):
    getStudentData()

# Sort List...
studentDataList.sort()

# Finally Print Table...
for item in studentDataList:
    print(item)
Related