0
a = range(20,30)
b = range(1000,5000)
list1 = [["range a",a],["range b",b]]
x = int(input())
for i in list1:
    if x in i:
        print("congratulations! input is in range"+i[0])
y = input()

The program closes immediately without displaying an error message

  • 4
    Possible duplicate of [SyntaxError: multiple statements found while compiling a single statement](https://stackoverflow.com/questions/21226808/syntaxerror-multiple-statements-found-while-compiling-a-single-statement) – MoxieBall Jun 28 '18 at 14:25
  • This is because of how you are typing your code into the interactive interpreter -- each line at the most unindented level needs to start on its own `>>>` line. If you put this code in a file, it runs fine (minus the fact that `x` is a string so it won't be in either of the ranges as you are expecting). – MoxieBall Jun 28 '18 at 14:27
  • It works on my machine.... (Pyhton 3.5.3). On which statement do you get the error? – Adrian W Jun 28 '18 at 14:28
  • What exactly you want to achieve with `if x in i`? Do you want to check that `x` is in both ranges? – kosnik Jun 28 '18 at 14:30
  • Actually `print("congratulasions! input is in range of "+ i)` gives error `TypeError: Can't convert 'range' object to str implicitly` – Adrian W Jun 28 '18 at 14:46
  • When I run it in the shell I get the error on the first line (wich is on the >>>) – David Ordman Jun 28 '18 at 14:46

2 Answers2

0

The below line of code works.

     a = range(20,30)
     b = range(1000,5000)
     list1 = [a,b]
     x = int(input())
     for i in list1:
    if x in i:
         print("congratulasions! input is in range of ",i)
    y = input()
Sakthivel G
  • 121
  • 1
  • 8
0

Looks like your if statement is checking a list the contains a list. Change it to if x in i[1]: and it should be all fine. Here is my code that works.

a = range(20,30)
b = range(1000,5000)
list1 = [["range a", a],["range b", b]]
x = int(input())
for i in list1:
    if x in i[1]:
        print (i[0])

Tested it in a IDLE with Python 3.7.0 and here is my direct output.

>>> a = range(20,30)
>>> b = range(1000,5000)
>>> list1 = [["range a", a],["range b", b]]
>>> x = int(input())
25
>>> for i in list1:
    if x in i[1]:
        print(i[0])

range a

If it still don't work for you maybe you have a problem in your IDE.

TysonU
  • 432
  • 4
  • 18