1

Based on reading the 2.7 documentation, the following code should step through correctly from what I can tell. I call raw_input(text) to get an input for variable age, but it always returns You can see rated R movies.

import sys

age = raw_input("What is your age? ")

if age >= 17:
    print "You can see rated R movies."

elif age < 17 and age > 12:
    print "You can see a rated PG-13 movies."

else:
    print "You can only see PG movies!"

Based on my logic, if the value passed to age is not greater than or equal to 17, it should move onto the next statement. That is not the result I get though. It always returns the Rated R line. For example I input 3 and it still gives me the rated R response. If I flip the > to < then it always returns the "You can only see PG movies!" line.

Thoughts?

2 Answers2

5

raw_input returns a string. you need to convert your input to an int before you can test it against numbers.

age = int(raw_input('How old are you? '))
sam-pyt
  • 1,027
  • 15
  • 26
  • **EDIT** Forget that last response. I fixed it and your answer was correct. Thank you! I understand now why that wasn't working. I didn't think about it like that, but once you pointed it out it made complete sense. – OutlawBandit Dec 04 '17 at 03:56
  • Yep, its correct. Mark the answer as correct, to help other people see it and to give him the deserved reputation. :) – Pat Dec 04 '17 at 04:21
  • This question should really be closed as a duplicate – OneCricketeer Dec 04 '17 at 05:00
0

When you use the raw_input method you are asking to enter a value by keyboard, everything that is entered will be read as a string not as an integer even if you are asking for an integer value.

Your solution:

age = int(raw_input("What is your age? ")). 

Parse to int no matter what introduced.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Franndy Abreu
  • 186
  • 2
  • 12