a = int(input("Введите значение a:")) b = int(input("Введите значение b:")) x = (input("Введите значение x:")) for p in range(a, b): if x in p: print(x/a+b) else: try: x/a+b except: print("Error") finally: print() 
 Traceback (most recent call last): File "Папка", line 10, in <module> if x in p: TypeError: argument of type 'int' is not iterable 
  • an element from a sequence of numbers is a number, and you want to use it as a sequence - Grundy
  • most likely there should have been a comparison operator: == - Grundy
  • If I understood correctly, and you really check in this code whether x is in the interval a ... b, then using brute force is a pretty bad decision. For integers, it will work, but it will do a lot of extra work (especially at large intervals). And the algorithm can not be expanded to fractional numbers, if such is needed. The correct solution here is to verify that x> a and x <b. - Xander

1 answer 1

In the line if x in p: You are trying to check whether x in p , and p is not a sequence, but a prime number. For verification, you must use the comparison:

 #!/usr/bin/env python3 a = int(input("Введите значение a: ")) b = int(input("Введите значение b: ")) x = int(input("Введите значение x: ")) for p in range(a, b): if x == p: print(x/a+b) else: try: x/a+b except: print("Error") finally: print() 

I also recommend inserting a space after the colons, when data is being entered, this is common practice. You also apparently forgot to cast the int parameter value x .

  • Thank you for your reply - Doper
  • If it is not difficult, mark this answer as accepted (you should have access to clicking the check mark under the up and down arrow to the left of the text of my answer). This is a common practice on stackoverflow in case the answer suits you. - lospejos
  • @Doper you can use: x in range(a, b) or a <= x < b instead of a loop. - jfs
  • Please do not copy the code from the question that demonstrates bad practices without explicitly mentioning it: 1- do not use bare except: (it catches even KeyboardInterrupt, SystemExit) better ZeroDivisionError explicitly use 2- for / else syntax is not needed at all - jfs
  • I agree that the code is not the best, but I didn’t set a goal to fully optimize the code, but focused only on the answer :-) As for for / else, it’s possible that some of the code was simply cut out for simplification, and in the full version this code does matter. However, in the form in which it is presented - I agree, if / else is superfluous here. - lospejos