Question

grade=[]
names=[]
highest=0



cases=int(input('Enter number of cases: '))
for case in range(1,cases+1):
    print('case',case)

    number=int(input('Enter number of students: '))
    for numbers in range (1,number+1):

        name=str(input('Enter name of student: '))
        names.append(name)
        mark=float(input('Enter mark of student:'))
        grade.append(mark)


    print('Case',case,'result') 
    print('name',list[list.index(max(grade))])
    average=(sum(grade)/number)
    print('average',average)
    print('highest',max(grade))
    print('name',names[grade.index(max(grade))])

I want to print name of the student with the highest mark. I have not learned anything other than list, while and for. NO dictionary ..nothing. I was wondering how can i do this? ALSO i am getting this error!!! builtins.AttributeError: 'str' object has no attribute 'append'. HELP. thank you! :D

No correct solution

OTHER TIPS

  1. for number in range (1,number+1):
    

    Don't reuse variable names for different things, call one of them numbers and the other number:

    numbers=int(input('Enter number of students: '))
    for number in range (1,numbers+1):
    
  2. You made name a list in the beginning:

    name=[]
    

    but here you assign a single input to it:

    name=str(input('Enter name of student: '))
    

    The you append the new name to itself:

    name.append(name)
    

    which is not possible, because name is after the input no longer a list but a string. Again using different variable names for different things would help. Call the array names and the single input name:

    names = []
    #...
    name=str(input('Enter name of student: '))
    names.append(name)
    
  3. And here:

     print('name',list[list.index(max(grade))])
    

    list is a build-in type, not one of your variables, so what you are trying to do is index a type, not a specific list. If you want to call index on a specific list you do so by using the variable name of that list. grade.index(...) will find the specific position matching the passed grade in grade and then you can use this position to get the corresponding name, because you know, that the name is at the same position in names:

    print('name',names[grade.index(max(grade))])
    

Here is a somewhat more elaborated version; working through it should give you a better feel for the language.

from collections import namedtuple
import sys

# Python 2/3 compatibility shim
if sys.hexversion < 0x3000000:
    inp, rng = raw_input, xrange    # Python 2.x
else:
    inp, rng = input, range         # Python 3.x

def type_getter(type):
    """
    Build a function to prompt for input of required type
    """
    def fn(prompt):
        while True:
            try:
                return type(inp(prompt))
            except ValueError:
                pass        # couldn't parse as the desired type - try again
    fn.__doc__ = "\n    Prompt for input and return as {}.\n".format(type.__name__)
    return fn
get_int   = type_getter(int)
get_float = type_getter(float)

# Student record datatype
Student = namedtuple('Student', ['name', 'mark'])

def get_students():
    """
    Prompt for student names and marks;
      return as list of Student
    """
    students = []
    while True:
        name = inp("Enter name (or nothing to quit): ").strip()
        if name:
            mark = get_float("Enter {}'s mark: ".format(name))
            students.append(Student(name, mark))
        else:
            return students

def main():
    cases = get_int("How many cases are there? ")
    for case in rng(1, cases+1):
        print("\nCase {}:".format(case))
        # get student data
        students = get_students()
        # perform calculations
        avg = sum((student.mark for student in students), 0.) / len(students)
        best_student = max(students, key=lambda x: x.mark)
        # report the results
        print(
            "\nCase {} average was {:0.1f}%"
            "\nBest student was {} with {:0.1f}%"
            .format(case, avg, best_student.name, best_student.mark)
        )

if __name__=="__main__":
    main()

which runs like:

How many cases are there? 1

Case 1:
Enter name (or nothing to quit): A
Enter A's mark: 10.
Enter name (or nothing to quit): B
Enter B's mark: 20.
Enter name (or nothing to quit): 

Case 1 average was 15.0%
Best student was B with 20.0%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top