Good day! Please tell me how to check the value when you enter n. When n> 20, the program should stop. He did all sorts of permutations with the cycle and if, nothing good happened. Task for Horner's Scheme ( problem condition ) And here is what I got:

n = int(input()) x = float(input()) i = 0 i = float(i) while n >= 0 and n < 21: coef = float(input()) n = n - 1 i = i + coef i = i * x if n == 0: break else: pass coef = float(input()) i = i + coef print(i) 

1 answer 1

Validation of input values ​​is best done separately from business logic:

  if (n < 0 || n > 20): sys.exit("Incorrect n value") 

For example:

 n = int(input()) if n < 0 or n > 20: sys.exit("Incorrect n value") x = float(input()) a = float(input()) result = a for i in range(0, n): a = float(input()) result *= x result += a print result