Search code examples
pythonif-statementconditional-statementsraw-input

Simple raw_input and conditions


I created the simple code:

name = raw_input("Hi. What's your name? \nType name: ")
age = raw_input("How old are you " + name + "? \nType age: ")

if age >= 21
    print "Margaritas for everyone!!!"
else:
    print "NO alcohol for you, young one!!!"

raw_input("\nPress enter to exit.")

It works great until I get to the 'if' statement... it tells me that I am using invalid syntax.

I am trying to learn how to use Python, and have messed around with the code quite a bit, but I can't figure out what I did wrong (probably something very basic).


Solution

  • It should be something like this:

    name = raw_input("Hi. What's your name? \nType name: ")
    age = raw_input("How old are you " + name + "? \nType age: ")
    age = int(age)
    
    if age >= 21:
        print "Margaritas for everyone!!!"
    else:
        print "NO alcohol for you, young one!!!"
    
    raw_input("\nPress enter to exit.")
    

    You were missing the colon. Also, you should cast age from string to int.

    Hope this helps!