Silly question about print.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vegtard
    New Member
    • May 2007
    • 7

    #1

    Silly question about print.

    hi. i'm very new to python. just started looking at some online amateur guides for fun a few days ago. anyways, today i was sitting here staring at the wall, and i decided to make probably the most useless script ever.

    what you do is, you type in the day of the month, the month and the year you were born, and the current day of the month, month and year, and it calculates your age, or whoever's information you wrote in. useless. utterly useless. :D

    ive been tinkering on it a bit, and ive come down to one last problem. heres what ive made:
    Code:
    a = 9           #day of the month you were born
    b = 11         #month you were born
    c = 1990      #year you were born
    d = 20         #today's day of the month
    e = 5           #this's month
    f = 2007       #this year
    
    if c > f:
     print 'you have written a faulty birth date or current date, please try again.'
    
    if b == e and c == f:
      g = (f-c)
      print 'congratulations, today is your birthday. today you turn (g).'
    
    if e > b:
       g = (f-c)
       print 'You are (g) years old.'
    
    if b > e:
        g = (f-c-1)
        print 'you are (g) years old.'
    
    if b == e and a > d: 
         g = (f-c-1)
         print 'you are (g) years old.'
    
    if b == e and d > a:
          g = (f-c)
          print 'you are (g) years old.'
    so my problem is i cant figure our how to make g appear in the print. ive tried a few things, but since i hardly know anything about python at this time, i'm turning to you for help.

    and as ive said, i'm a complete amateur so no need to commend the fact that my script probably blows.

    :)
    Last edited by bartonc; May 20 '07, 11:17 PM. Reason: added [code][/code] tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The string format operator (%) will do what you need.[code=Python]>>> g = 17
    >>> print 'You are %s years old' % g
    You are 17 years old
    >>> m = 6
    >>> d = 12
    >>> print 'You are %s years, %s months and %s days old.' % (g,m,d)
    You are 17 years, 6 months and 12 days old.
    >>> [/code]

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by vegtard
      Code:
      if b == e and d > a:
            g = (f-c)
            print 'you are (g) years old.'
      so my problem is i cant figure our how to make g appear in the print. ive tried a few things, but since i hardly know anything about python at this time, i'm turning to you for help.

      and as ive said, i'm a complete amateur so no need to commend the fact that my script probably blows.

      :)
      It's actually not such a silly question. String Format techniques are definitely the way to go (as my friend BVDet has show), but it's not something that is taught right out of the gate. Most begginners start by doing stuff (which works, but is not as elegant) like this:
      Code:
       age = 44
      print "Your age is " + str(age) + "years old."
      The section of the Python manual is called String Formatting Operations and can be found by searching Start>Programs> Python x.x>Python Manuals or here.

      Please not the "reason for editing" below. To use code tags, make sure the Enhanced Mode radio button is selected, the click the # button, then paste you code the click the # button again. You'll be able to see what they look like in this post when you reply to it.
      Last edited by bartonc; May 20 '07, 11:31 PM. Reason: added [code][/code] tags

      Comment

      Working...