EOFError: EOF when reading a line error in my function

Question:

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?

Asked By: Disobey1991

||

Answers:

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}')
Answered By: Barmar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.