1

I'm learning Python and am playing with simple code writing and if-elif-else statements at the moment. I have this code block which is always returning the else statement, even if I put in a number between 1 and 30.

elif door == "3":
        print "This is a winning treasure room, congratulations!"
        print "Pick a number between 1 and 30"

        number = raw_input ("> ")

        if number in range (1,10):
            print "that's a number between 1 and 10"

        elif number in range (11, 20):
            print "that's a number between 11 and 20"

        elif number in range (21, 30):
            print "that's a number between 21 and 30"

        else:
            print "that's not a number we asked for"

I've also then tried:

elif door == "3":
        print "This is a winning treasure room, congratulations!"
        print "Pick a number between 1 and 30"

        number = raw_input ("> ")

        if number == number in range (1,10):
            print "that's a number between 1 and 10"

        elif number == number in range (11, 20):
            print "that's a number between 11 and 20"

        elif number == number in range (21, 30):
            print "that's a number between 21 and 30"

        else:
            print "that's not a number we asked for"

I also tried:

 if number == x in range (1, 10):

But of course x isn't defined which comes up as an error.

Any guidance appreciated.

arumiat
  • 123
  • 2
  • 9
  • 2
    Welcome to Python! I guess the number when read from input is a string, try `int(number)` also a good opportunity to learn robust input handling with try and except ;-) – Dilettant Jun 14 '16 at 10:46
  • 4
    "that's a number between 1 and 10" not including 10 – warvariuc Jun 14 '16 at 10:48

1 Answers1

3

raw_input() converts the input into string. You need to convert it into integer.

number = int(raw_input ("> "))

raw_input():

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

from doc

gaganso
  • 2,914
  • 2
  • 26
  • 43
  • 2
    Excellent, I've learnt about this but it didn't occur to me. I also realised that a range of course ignores the last number in the range so picking '10' will skip to the else statement as well. Will work on this now. Thanks again – arumiat Jun 14 '16 at 11:05