Having trouble with the for loop and using an if statement


#1

I am suppose to use one “for loop” and one “if statement” to
print out all numbers from 0 that are less than the user’s inputted age.

It tells me that my elif is wrong. I dont see anything wrong about it


age = int(raw_input("Enter your age: "))

if age > 18:
    print "You are old enough"
elif < 16:
    print "Almost there"
else "You're just too young"

if for i in range(0,age) > 18 :
    print "You are old enough!"


#2

is this part of an exercie?

why do you need a if statement to do this? You can simply do:

for i in range(0,age):
   print i

i don't see where the if statement comes in if i understand the instructions correctly


#3

I am suppose to check if the users age is 18 or older in the if statement @stetim94


#4

okay, but then you would simple nest the for loop in the if statement:

if age > 18:
  # for loop here

then you should be good?


#5

So like this ? @stetim94

if age > 18:
for i in range(0,age):
   print i

#6

no, the indent is off, something which is nested inside a if statement should be indented:

if age > 18:
   for i in range(0,age):
      print i

i on purpose indented the comment


#7

If I just ran it without the loop and that if statement below. it tells me that my elsif is unknown.
@stetim94

age = int(raw_input("Enter your age: "))

if age > 18:
    print "You are old enough"
elif < 16:
    print "Almost there"
else "You're just too young"

#8

16 greater then what?

a colon after else is missing


#9

I've fixed that then i got another error
@stetim94

i used age by the elsif age > 16

File "<stdin>", line 7
    else "You're just too young"
                               ^
SyntaxError: invalid syntax

#10

already catched that one?


#11

Its still not working corectly after i ran it I entererd in the age then it just gives me another line to type in the console. @stetim94

And this was after I made the change you with the semi colon.


#12

i didn't say anything about a semi-colon? a said a colon is missing after else:

else:

the colon (:) is missing in your case


#13

yes I meant that the colon sorry @stetim94

Updated code:

age = int(raw_input("Enter your age: "))

if age > 18:
    print "You are old enough"
elif age > 16:
    print "Almost there"
else: "You're just too young"

if age > 18:
   for i in range(0,age):
      print i

#14

this code works fine, refresh the page.


#15

It works without a print statement?

else: "You're just too young"

Or does it have to be

else: 
    print "You're just too young"

I mean if it works, it's alright but I'm just curious.


#16

oh, good point, i missed it, a print keyword should be included:

else: print "You're just too young"

@datfatcat, good catch :smiley:


#17

The problem is I need to say which variable you are checking to see if greater than 18 again I can't just use "age" in the if statement and not reference it in the the elif statement.
@datfatcat
@stetim94


#18

but this code:

age = int(raw_input("Enter your age: "))

if age > 18:
    print "You are old enough"
elif age > 16:
    print "Almost there"
else: "You're just too young"

if age > 18:
   for i in range(0,age):
      print i

works fine, right?


#20

age is referenced in both the if and elif statement (at least that's what I see in the code @stetim94 provided). Was there something else that's off?


#21

Its cool I corrected it with your help thanks. @stetim94 @datfatcat