0

I'm new to python and this is what i came up with, it does not work.

age = input("How old are you? ")

if age < 18
    print("You are under age.")
else
    print("You are over age")

Thanks.

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • 2
    `input()` returns string. cast it to an integer: `age = int(input("How old are you? "))` – Andrej Kesely Jul 09 '20 at 21:11
  • 2
    in addition to https://stackoverflow.com/users/10035985/andrej-kesely 's comment, you are missing colons after your `if` and `else` – Red Twoon Jul 09 '20 at 21:14
  • Welcome to Stack Overflow. Please take the [tour] and read [ask]. This code will have produced a _useful error message_ that you have not included here. Please _always **read** and **share** error messages_. They're not there for fun; they are actually helpful. – ChrisGPT was on strike Jul 09 '20 at 21:24

2 Answers2

1

The result of the input function must be convert to an int by using the int constructor, like int("123") in order to be compared to the number 18.

if int(input("How old are you?")) < 18:
  print("You are under age")
else:
  print("You are over age")
Sudoku8626
  • 28
  • 1
  • 4
1

What is the type of input? Hmm let's open the REPL and see.

$ ipython
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.6.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: age = input('how old are you?')
how old are you?10

In [2]: type(age)
Out[2]: str

In [5]: age == '10'
Out[5]: True

In [6]: age == 10
Out[6]: False

See how Python treats the type str different from int?

You're also forgetting colons : after your if statememt

Jacques
  • 927
  • 9
  • 18
Skam
  • 7,298
  • 4
  • 22
  • 31