0

Getting mathematical function as user's input in Python

I used f = lambda x: input() to get a mathematical function as user's input, but when I put this in a for loop, the function is requested in every iterating. How do I store the function in a variable only once and use it in the loop? from math import cos, sin, pow, e n = int(input('Number of elements in partition: ')) print('The range is set at ...... ') minimum = int(input('Minimum: ')) maximum = int(input('Maximum: ')) f = lambda x: eval(input('Enter a funtion: ')) fraction = (maximum - minimum) / (n-1) UpperSum = 0 LowerSum = 0 for i in range(1, n): UpperSum = UpperSum + fraction * f(fraction * i) LowerSum = LowerSum + fraction * f(fraction * (i - 1)) print(f'UpperSum: {UpperSum}\nLowerSum {LowerSum}')

26th Aug 2019, 10:46 PM
Mariana Ferreira
1 Answer