-3

So I am getting an invalid syntax error when trying to run this little guy,

age = input("Please tell me your age?: ")
age_test = eval(age)
if age_test < 1:
    print("You can't be negative!")
    if age_test > 100:
        print("You can't be over 100!")
        else:

Can someone help me out here? I'm not new to programming but I am new to python and I don't see anything wrong.

DaltonM
  • 39
  • 1
  • 4
  • 2
    you have an `else` statement without an `if`. move it back to match the previous `if` and if you want to do nothing with the else then remove it entirely – SirParselot Jan 27 '16 at 16:15
  • 1
    Also you 2nd `if` will never be true. – Klaus D. Jan 27 '16 at 16:18
  • 2
    Consider using `int` instead of `eval` to turn strings into integers. As-is, your user can enter `__import__("os").system("rm -rf /")` at the input prompt and delete your hard drive. – Kevin Jan 27 '16 at 16:23
  • Thank you guys! Y'all answered so quick! – DaltonM Jan 27 '16 at 16:33

5 Answers5

0

Indentation in python works almost like how curly braces in most other languages work. 4 spaces signals to the compiler that the block is indented, and so your code is actually nesting each conditional statement within one another, rather than doing what I think you intended, which was to have them assessed one after another.

Unindent the conditionals to the same line as the initial if statement and go from there.

Barnabus
  • 376
  • 3
  • 14
0

In Python, indenting is important. Your else clause is indented at the level of the preceding print("You can't be over 100!") statement. Hence, there needs to be an if statement at that level. There isn't, so you'll get a syntax error.

So, as SirParselot says, you need to move that else clause to the level of one of the preceding if's.

tmark
  • 33
  • 4
0

Use correct indentation:

age = input("Please tell me your age?: ")
age_test = eval(age)
if age_test < 1:
    print("You can't be negative!")
elif age_test > 100:
    print("You can't be over 100!")
else:
    do_other_stuff()
The Godfather
  • 4,235
  • 4
  • 39
  • 61
0

if, ifelse and else must be at the same indentation level, that is the reason of the error

Quoting:

A block is group of statements in a program or script. Usually it consists of at least one statement and of declarations for the block, depending on the programming or scripting language. A language, which allows grouping with blocks, is called a block structured language. Generally, blocks can contain blocks as well, so we get a nested block structure. A block in a script or program functions as a mean to group statements to be treated as if they were one statement. In many cases, it also serves as a way to limit the lexical scope of variables and functions.

Python uses a different principle. Python programs get structured through indentation, i.e. code blocks are defined by their indentation. Okay that's what we expect from any program code, isn't it? Yes, but in the case of Python it's a language requirement not a matter of style. This principle makes it easier to read and understand other people's Python code.

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

So as mentioned already, pay attention to intendation. Here's example of working code snippet:

age = input("Please tell me your age?: ")
age_test = int(age)

if age_test < 1:
    print("You can't be negative!")

if age_test > 100:
    print("You can't be over 100!")
else:
    print("Your age is %s" %age_test)
G4mo
  • 597
  • 1
  • 8
  • 18