Найти - Пользователи
Полная версия: Warning noob. не могу найти ошибку.
Начало » Python для новичков » Warning noob. не могу найти ошибку.
1
sick_noob
Привет. На сайте Udacity.com на примере питона объясняют как работает поисковая машина.
Не могли бы мне помочь понять почему в следующем коде ошибка? Первый элемент в листе общего это название университета, второй количество студентов, третий оплата за обучение каждый студентом.
Надо найти общее количество студентов во всех университетах и сколько они платят за обучение все вместе взятые.
получаю такую ошибку

(<function total_students at 0xb75fa5dc>, <function total_tuition at 0xb75fa614>)


#Define a procedure, total_enrollment,
#that takes as an input a list of elements,
#where each element is a list containing
#three elements: a university name,
#the total number of students enrollect,
#and the annual tuition.
#The procedure should return two numbers,
#not a string,
#giving the total number of students
#enrolled at all of the universities
#in the list, and the total tuition
#(which is the sum of the number
#of students enrolled times the
#tuition for each university).
udacious_univs = [['Udacity',90000,0]]
usa_univs = [ ['California Institute of Technology',2175,37704],
              ['Harvard',19627,39849],
              ['Massachusetts Institute of Technology',10566,40732],
              ['Princeton',7802,37000],
              ['Rice',5879,35551],
              ['Stanford',19535,40569],
              ['Yale',11701,40500]  ]
#>>> print total_enrollment(udacious_univs)
#(90000,0)
#>>> print total_enrollment(usa_univs)
#(77285,3058581079L)
def total_enrollment(p):
    def total_students(p):
        total = 0
        for e in p:
            total = total + e[1]
        return total
    def total_tuition(p):
        total = 0
        for e in p:
            total = total + e[1] * e[2]
        return total    
    return total_students, total_tuition
print total_enrollment(usa_univs)

Спасибо за помощь.
reclosedev
в total_enrollment объявляются еще две функции total_students и total_tuition. Сейчас они просто возвращаются:
return total_students, total_tuition
а должны возвращаться результаты их вызова:
return total_students(p), total_tuition(p)
pyuser
Какой-то не питоновский код :(
Так наверно будет лучше:
def total_enrollment(data):
    tmp = ((n, n * m) for _, n, m in data)
    return list(map(sum, zip(*tmp)))
fata1ex
pyuser
Какой-то не питоновский код :(
Так наверно будет лучше

Там питоном учат людей программированию (cs101), поэтому код и не питоновский :)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB