from math import factorial
factorial(1)
factorial(0)
from math import factorial, exp
# validate user input, make them enter a number between 0 and 10
# set input as invalid
x = -1
# while not (what you want):
while not (x>=0 and x<=10):
# ask for input
x_str = input('Enter a number between 0 and 10: ')
# run any code on the input
x = float(x_str)
approx = 0
i=0
while not (abs(exp(x)-approx)<0.001):
try:
term = (x**i)/(factorial(i))
except:
break
approx = approx + term
i = i + 1
print(f'approximation: {approx}')
print(f'using math.exp(): {exp(x)}')
print(f'error: {abs(exp(x)-approx)}')
print(f'The loop ran {i+1} times')
Enter a number between 0 and 10: -1 Enter a number between 0 and 10: 12 Enter a number between 0 and 10: 2 approximation: 7.3887125220458545 using math.exp(): 7.38905609893065 error: 0.0003435768847959153 The loop ran 11 times