Hello everyone it Sam again.I finally understand how to use the conditional statement so i decided to write a program that allow users of age 21 and above access a web page but some where long the line it broke and let any age access the web page.need help debugging it pls Age = raw_input("Enter Age : ") if Age < 21: print ("You can't access this web page") elif Age == 21: print ("Go ahead welcome to this web page") elif Age > 21: print ("Welcome to our web page")
Whenever you have any `elif` statements in Python 2.7.x, you must finish with an `else` clause to be syntactically correct.
after adding to my block of codes else ie Age = raw_input("Enter Age : ") if Age < 21: print ("You can't access this web page") elif Age == 21: print ("Go ahead welcome to this web page") elif Age > 21: print ("Welcome to our web page") else: print ("no permit for you") i ran the program and it produced this Enter Age : 12 Welcome to our web page >>> why
what do i do about this program running and producing the wrong output
`raw_input` interprets numbers as strings. You will want to make sure to convert your input to either a float or int if you want to do comparisons with it. On a side note, remember that ``` if x > 0: #do something elif x <= 0: #do something else ``` is the same as ``` if x > 0: #do something else: #do something else ```
do numerical operations with it*
The print statements in your program look like Python3. In Python3 raw_input() is deprecated, ( not recommended, will probably be removed in the future ). input() is the preferred way to get user input. In Python2, raw_input() returns a string that the user input. Like TuringTest said, you need to convert it to a numerical type before doing numerical comparisons. This line may be a little dense for you, but in this case this is how I would code it: Age = int( raw_input("Enter Age : ") ) A couple of other things: Python2 doesn't use parenthesis in print statements, that's why I think you're using Python3. By convention ( Python will accept it, but programmers have decided.... ) variable and function names start with a lowercase letter. Names of classes start with uppercase. I'll put constants ( variables that don't change ) in all caps.
@Turing Test and @rsmith6559 you guy are the best my program runs correctly now like you said, raw_input returns to string.I followed your instruction and it worked.And @rsmith6559 i use python 2.7.9.I only used used parenthesis Because i thought that was where my problem was.I guess i was losing it.Thanks again.
Join our real-time social learning platform and learn together with your friends!