Професионална програма
Loading...
+ Нов въпрос
d.atanasoff avatar d.atanasoff 1 Точки

Runtime error на 01. Bonus Scoring System

Здравейте реших https://judge.softuni.bg/Contests/Practice/Index/2028#0

но ми дава  90/100 runtime error на 

Test #6 (Runtime error)

Time used: 0.056 s

Memory used: 8.37 MB

import math
students = int(input())
lectures = int(input())
initial_bonus = int(input())
attendance_list = []

for i in range(1, students + 1):
    attendances = int(input())
    attendance_list.append(attendances)

print(f'Max Bonus: {math.ceil((max(attendance_list) / lectures) * (5 + initial_bonus))}.')
print(f'The student has attended {max(attendance_list)} lectures.')

 

 

Тагове:
0
Python Fundamentals
MartinBG avatar MartinBG 2680 Точки
Best Answer

Има тест с 0 лекции, заради който решението гърми с exception за делене на 0: 

(max(attendance_list) / lectures)

Примерен вариант за заобикаляне на проблема (разбира се, може да се разпише и по-красиво):

import math
students = int(input())
lectures = int(input())
initial_bonus = int(input())
attendance_list = []

if lectures == 0:
    print(f'Max Bonus: 0.')
    print(f'The student has attended 0 lectures.')
else:
    for i in range(1, students + 1):
        attendances = int(input())
        attendance_list.append(attendances)
    
    print(f'Max Bonus: {math.ceil((max(attendance_list) / lectures) * (5 + initial_bonus))}.')
    print(f'The student has attended {max(attendance_list)} lectures.')

 

0
d.atanasoff avatar d.atanasoff 1 Точки

Благодаря!

1
AliYK avatar AliYK 5 Точки

Здравейте! Имате ли представа защо въпреки че сложих проверка за деление на 0, пак получавам същия runtime error?

import math
students_count = int(input())
lectures_count = int(input())
additional_bonus = int(input())

max_bonus = 0
total_bonus = 0

if lectures_count == 0:
    print(f'Max Bonus: 0.')
    print(f'The student has attended 0 lectures.')
else:
    for student in range(students_count):
        attendances = int(input())
        total_bonus = attendances / lectures_count * (5 + additional_bonus)
        if total_bonus >= max_bonus:
            max_bonus = total_bonus
            max_bonus_attendances = attendances

print(f'Max Bonus: {math.ceil(max_bonus)}.')
print(f'The student has attended {max_bonus_attendances} lectures.')

 

 

0
MartinBG avatar MartinBG 2680 Точки

@AliYK

Последните два реда от кода трябва да бъдат в тялото на else-a, тъй като max_bonus_attendances ще съществува само, ако се влезе в else-a.

Допълнително, max_bonus_attendances е правилно да бъде деклариран още преди обхождането на судените, защото ако students_count е 0 програмата отново ще гръмне.

 

Оправеният код:

import math
students_count = int(input())
lectures_count = int(input())
additional_bonus = int(input())

max_bonus = 0
total_bonus = 0

if lectures_count == 0:
    print(f'Max Bonus: 0.')
    print(f'The student has attended 0 lectures.')
else:
    max_bonus_attendances = 0
    for student in range(students_count):
        attendances = int(input())
        total_bonus = attendances / lectures_count * (5 + additional_bonus)
        if total_bonus >= max_bonus:
            max_bonus = total_bonus
            max_bonus_attendances = attendances

    print(f'Max Bonus: {math.ceil(max_bonus)}.')
    print(f'The student has attended {max_bonus_attendances} lectures.')

 

Ето и по-кратко решение:

import math
students_count = int(input())
lectures_count = int(input())
additional_bonus = int(input())

max_bonus = 0
max_bonus_attendances = 0

if lectures_count > 0 and students_count > 0:
    for student in range(students_count):
        attendances = int(input())
        total_bonus = attendances / lectures_count * (5 + additional_bonus)
        if total_bonus >= max_bonus:
            max_bonus = total_bonus
            max_bonus_attendances = attendances

print(f'Max Bonus: {math.ceil(max_bonus)}.')
print(f'The student has attended {max_bonus_attendances} lectures.')

 

0
30/06/2020 23:56:21