Skip to content
Advertisement

EOFError: EOF when reading a line error in my function

I wrote this function which takes a list of numbers and returns their sum and product:

from typing import List, Tuple
import math
from functools import reduce
import operator

def sum_and_product(numbers: List[float]) -> Tuple[float, float]:

    a = numbers.split(" ")
    b = list(map(float, a))
    c = math.fsum(b)
    d = reduce(operator.mul, b, 1)
    
    return c, d

print(f'Sum:{sum_and_product(input())[0]:>8.2f}')
print(f'Product:{sum_and_product(input())[1]:>8.2f}')

input:

1.5 2.0 4.0

output:

Sum: 7.50
Product: 12.00

the problem is that the two print functions are not working. It only prints one of them and for the other I have the EOFError: EOF when reading a line. Do you have any suggestion to change the function in order to don’t have this error?

Advertisement

Answer

You’re calling input() twice, and the second one will try to read another line of input. It’s getting an error because there’s no more input available (I assume you’re using an automated tester — if you run this interactively it will just wait for you to type another line).

Call the function just once, and assign the results to variables. Then you can print the sum and product from this.

s, p = sum_and_product(input())
print(f'Sum: {s:>8.2f}')
print(f'Product: {p:>8.2f}')
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement