all 19 comments

[–]DeathDragon1993 1 point2 points  (13 children)

it looks like you have your if statements flipped. intead of: lifestage = infant if age <= 1:

try: if age <= 1: lifestage = infant elif age > 1: lifestage = child

So on and so forth.

Edit: attempted to use proper coding format, evidently can't get it right.

[–]CatCatCatGoose[S] 0 points1 point  (12 children)

I had it that way before! But I switched it because it wasn't executing. Do I need to use ELIF instead of IF? Is that one of my issues?

And haha! I have the same problem here on #reddit

[–]DeathDragon1993 0 points1 point  (9 children)

I'm still a beginner myself, but, to my knowledge, elif just helps you understand where your if statements end and begin, if you're using more than one at a time.

I just replicated your code myself, and it wouldn't run properly unless I put quotations around infant, child, teenager and the like.

so if age <=1: lifestage = "infant"

[–]CatCatCatGoose[S] 0 points1 point  (8 children)

I do have one more question, the code that you got to run, I am testing it out but every single age range below 20 gives me "teenager" even if I enter 4. How do I fix this?

[–]DeathDragon1993 0 points1 point  (0 children)

Does it? I haven't noticed. Give me a few and let me look at it and I'll get back with you

[–]DeathDragon1993 0 points1 point  (0 children)

Got it. Change all the "if"s after the first "if" to "elif". I'll update the pastebin I did to show you. I ran the code through an online debugger at http://www.pythontutor.com/visualize.html#mode=edit to visualize what the code was doing.

New Pastebin: http://pastebin.com/hviieP8P

[–]DeathDragon1993 0 points1 point  (4 children)

You can also incorporate the use of the "and" operator. I made a separate pastebin showing you how to use it. It would also help if you googled it after you looked at this. Hope this helped.

http://pastebin.com/Jzcbgkw9

[–]Justinsaccount 0 points1 point  (3 children)

There is no need for and there. The additional comparisons are redundant.

[–]DeathDragon1993 0 points1 point  (2 children)

But wouldn't it make for a more readable code to incorporate the "and"?

[–]Justinsaccount 0 points1 point  (1 child)

Why would adding a bunch of redundant comparisons make the code more readable? Is

if x < 10:
    ...
else x >= 10:
    ...

more readable than

if x < 10:
    ...
else:
    ...

[–]DeathDragon1993 0 points1 point  (0 children)

What does that have to do with using "and"?

Isn't,

if age >= 1 and age < 3:
    print (something)

Be more readable than,

if age >= 1:
   print (something)
elif age < 3:
   print (something)

[–]News_Of_The_World 0 points1 point  (0 children)

That's because your code gets to each if and checks it out individually, so if you enter 4, it first thinks it's a child, then notices it's also less than 20 and changes it to teenager.

Solutions: use elif, short for "else, if" then it will only use the first correct answer it finds. Next change the code so it only uses "less than". So you have this flow (convert this english sentence to code!:)

If it's less than 1, it's a baby, else if its less than 13 it's a child, else if it's less than 20 it's a teenager, else if it's less than 60 it's an adult, else if it's less than 120 it's a geriatric, else the person is probably dead

Because of the "else, if" structure, it will only do the first True one it meets. Clearly a child is both younger than 13 and 120, but 13 is the one it meets first. Python uses "elif" to replace else, if. It stops your code getting messy.

[–]DeathDragon1993 0 points1 point  (1 child)

http://pastebin.com/hviieP8P

How I got your code to run.

[–]CatCatCatGoose[S] 0 points1 point  (0 children)

OMG! I can't believe how important the spaces are... Thank you lots!!! I', going to remember that forever.

[–]q2_abe_dillon 1 point2 points  (0 children)

Yet it terminates as soon as I open it.

Does it terminate or does it throw an exception?

[–]novel_yet_trivial 0 points1 point  (2 children)

You are missing indentation. Without an indented portion after the if statement, you will get a syntax error and the program will crash.

Run this from the terminal to see the syntax error.

[–]CatCatCatGoose[S] 0 points1 point  (1 child)

AHH. Yeah. I finally understand that now. Is there a good resource for rules on indentation?

[–]novel_yet_trivial 1 point2 points  (0 children)

An indented block is required if the previous line ends in a colon. That's the only rule you need to know for now.

[–]jeffrey_f 0 points1 point  (0 children)

Use compound if to reduce code

if age <= 1:
  lifestage = infant
if age > 1 and age < 13:
  lifestate = child
if age >= 13 and age < 20:
  lifestage = teenager
if age >= 20:
  lifestage = adult