0
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

user3295029
  • 1
  • 1
  • 4
  • 5
    this again.. There are atleast 3 questions posted today for this assignment – M4rtini Feb 11 '14 at 01:29
  • Errr.. i don't know about that. I am done programming. Just one error. :) – user3295029 Feb 11 '14 at 01:30
  • 2
    you're overwriting the list `name` when you take the name input. that's the problem, and why the append fails. – M4rtini Feb 11 '14 at 01:31
  • 3
    Did your teacher tell the whole class to go to stackoverflow for help or something? :P – M4rtini Feb 11 '14 at 01:34
  • could you please elaborate @M4rtini I did not really understand you. How can i append names so i can take them out with the corresponding highest mark index? thanks – user3295029 Feb 11 '14 at 01:36
  • The names and grades list are correlated, corresponding name-mark elements have the same index. So if you have the index of the highest grade, the same index should give you the corresponding name. – M4rtini Feb 11 '14 at 01:42
  • yeah that is what i am trying to do. I changed list name from 'name' to 'names'. That error disappeared but i get this now->File "None", line 25, in builtins.TypeError: descriptor 'index' requires a 'list' object but received a 'float'. This is the only way i know how to get the index of highest float – user3295029 Feb 11 '14 at 01:45
  • `grade.index(max(grade))` will get you the index. You have to use the function on the list you want the results from. Ie, the list that you want to extract the index from. `.index` is a function\method of the list object. – M4rtini Feb 11 '14 at 01:47
  • i did that but i get the error builtins.TypeError: descriptor 'index' requires a 'list' object but received a 'float' print('name',names[grade.index(max(grade))]) – user3295029 Feb 11 '14 at 02:05

2 Answers2

1
  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))])
    
  • i changed name to names and number to numbers. If you see i put a formula for average in 4th line from the lowest. The last part where you say don't use list as one of your variable. I am confused here. :/ – user3295029 Feb 11 '14 at 01:49
  • @user3295029 Yes correct I totally missed the line for `average`. –  Feb 11 '14 at 01:49
  • @user3295029 I expanded my answer a bit. –  Feb 11 '14 at 01:56
  • oh thanks nabla for expanding your answer but even when i use the edited version. i get the error builtins.TypeError: descriptor 'index' requires a 'list' object but received a 'float' – user3295029 Feb 11 '14 at 02:03
  • @user3295029 I don't see where this comes from if your whole code is the one posted in the question with only the suggested edits. Please add the whole current code producing the new error to the question. –  Feb 11 '14 at 02:10
  • posted the most current one! – user3295029 Feb 11 '14 at 02:19
  • you forgot to remove the old code `print('name',list[list.index(max(grade))])` is still there and is what is throwing the error – M4rtini Feb 11 '14 at 02:22
  • NABLA and martini if i could kiss you all I WOULD!!! THANKS! Computer programming is tricky. you forget '1' thing and the whole thing goes to crap! But thanks guys :) – user3295029 Feb 11 '14 at 02:23
  • No problem :) Finding these silly mistakes is a lot easier if you carefully read the error messages you get. Together with the error message, there should have been some information about where the error occurred, line number and the code at that line. – M4rtini Feb 11 '14 at 02:29
0

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%
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99