0

Hello everyone I am stuck on my homework for python and I have searched the forum and have tried to figure it out for three hours now. I still can't find an answer or figure it out. It might be there but my lack of knowledge may have kept me from seeing it.

I'm using python version 3.2.3.

I cannot figure out how to display the results from the if statement in my print statement. So if the persons age is < 40 it says "you are young" and if the persons age is > 40 it prints "you look great". I easily have it print the first time but then I need it to print at the end where msg is located I cannot figure out how. I included all the code in case I am not explaining this right.

print("Hello", firstname, lastname, "you're", age, "years old", msg)

Thanks for the help in advance and again I apologize if the answer is already here somewhere

# ITP 100 Python Programming
# In Class Challenge September 10, 2012

# Getting users name and age as input
firstname = input("Hello. What is your first name? ")
lastname = input("What is your last name? ")

birthyear = input("What year where you born? ")
birthyear = int(birthyear)

age = 2012 - birthyear
print("\nYou're", age, "years old.")

if age < 40: 
    print("you are young.")

else:
    print("You look great.")


print("Hello", firstname, lastname, "you're", age, "years old", msg) 


input("\n\nPress n to exit")
David Robinson
  • 77,383
  • 16
  • 167
  • 187
Chris
  • 1
  • 1
  • You want to display either "you are young" or "you look great" (depending on the value of age) at the end of the print("Hello, ...) statement is that it? – Borgleader Sep 13 '12 at 00:01
  • Since this is homework, I'll try to ask leading questions: Where are the values from each of the variables in the final print statement coming from? – Bubbles Sep 13 '12 at 00:04
  • 1
    Or you can use the [python ternary operator][1] print "A" if age < 40 else "B". [1]: http://stackoverflow.com/questions/394809/python-ternary-operator – Hans Then Sep 13 '12 at 01:16
  • 3
    Ironically, as of today the homework tag [has been rendered obsolete](http://meta.stackexchange.com/questions/147100/trogdor-ate-my-homework-tag)- so in fact no one *should* use it in the future. – David Robinson Sep 14 '12 at 16:46
  • 1
    Thankyou David for letting us know. – Chris Sep 14 '12 at 21:19

4 Answers4

0

You can create a variable that stores a string (your statement either "you are young." or "You look great.", or any string you want!) and display it later in your script. You have already assigned strings to variables earlier in your script when you create firstname and lastname.

dm03514
  • 54,664
  • 18
  • 108
  • 145
0

The problem is, you haven't initialized the variable msg. You can't use it yet, because until the print statement, it hasn't shown up in the program. What you should do is store the "You are young" etc. messages in a variable, such as msg. That way you can access it later.

jcolen19
  • 318
  • 3
  • 11
0

What about that ?

if age < 40: 
    msg = "you are young."
else:
    msg = "You look great."
print(msg)

Now, you have defined msg and you can use it later in your final print. And now, for an exercise: find a way to do that automatically when the user enters an age: you would define a function that takes a integer birthyear as input and would output a given message depending on the age...

Pierre GM
  • 19,809
  • 3
  • 56
  • 67
  • Thanks! I was trying to assign it but in the wrong way and I got an error. I was going crazy and I misunderstood the pseudo code too which didn't help. Thanks for the help! – Chris Sep 13 '12 at 22:25
0

You can do something like this:

print("You " + "are young." if age < 40 else "look great.")
Craig Wright
  • 1,575
  • 1
  • 11
  • 19