By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

Silly question about print.

P: 7
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:
Expand|Select|Wrap|Line Numbers
  1. a = 9           #day of the month you were born
  2. b = 11         #month you were born
  3. c = 1990      #year you were born
  4. d = 20         #today's day of the month
  5. e = 5           #this's month
  6. f = 2007       #this year
  7.  
  8. if c > f:
  9.  print 'you have written a faulty birth date or current date, please try again.'
  10.  
  11. if b == e and c == f:
  12.   g = (f-c)
  13.   print 'congratulations, today is your birthday. today you turn (g).'
  14.  
  15. if e > b:
  16.    g = (f-c)
  17.    print 'You are (g) years old.'
  18.  
  19. if b > e:
  20.     g = (f-c-1)
  21.     print 'you are (g) years old.'
  22.  
  23. if b == e and a > d: 
  24.      g = (f-c-1)
  25.      print 'you are (g) years old.'
  26.  
  27. if b == e and d > a:
  28.       g = (f-c)
  29.       print 'you are (g) years old.'
  30.  
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.

:)
May 20 '07 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
The string format operator (%) will do what you need.
Expand|Select|Wrap|Line Numbers
  1. >>> g = 17
  2. >>> print 'You are %s years old' % g
  3. You are 17 years old
  4. >>> m = 6
  5. >>> d = 12
  6. >>> print 'You are %s years, %s months and %s days old.' % (g,m,d)
  7. You are 17 years, 6 months and 12 days old.
  8. >>> 
May 20 '07 #2

bartonc
Expert 5K+
P: 6,596
Expand|Select|Wrap|Line Numbers
  1. if b == e and d > a:
  2.       g = (f-c)
  3.       print 'you are (g) years old.'
  4.  
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:
Expand|Select|Wrap|Line Numbers
  1.  age = 44
  2. 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.
May 20 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.