473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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:
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
2 987
bvdet
2,851 Expert Mod 2GB
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
6,596 Expert 4TB
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

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

Similar topics

14
by: Marcin Ciura | last post by:
Here is a pre-PEP about print that I wrote recently. Please let me know what is the community's opinion on it. Cheers, Marcin PEP: XXX Title: Print Without Intervening Space Version:...
0
by: s-galit | last post by:
hi, im trying to print my dataSet i created a printHandler and i connected it to the dataSet like this- printObj.DataSetToPrint = ds.Tables(1) -->error //by the way i have two tables in the...
1
by: Paul Tsai | last post by:
Dear All, I have a .htm file, and my question is how should I code if I want to print this htm file to the printer !! Thanks !! Paul
9
by: TY | last post by:
Hi all, I have this little simple script: for i in range(10): for j in range(5000000): pass # Timing-delay loop print i When you run it, it behaves as you would expect -- it prints 0...
2
by: Phoe6 | last post by:
print and softspace in python In python, whenever you use >>>print statement it will append a newline by default. If you don't want newline to be appended, you got use a comma at the end (>>>print...
2
by: meromake | last post by:
Hello when i print page html with Microsoft Explorer The path of file show in end of page what can hid the path
0
by: WaterWalk | last post by:
Until Python 2.5, the exception object still uses ansi string. Thus, in the following example: f = open(u"\u6d4b.log") Suppose the file to open does not exist, the output message of the...
1
by: Jean-Paul Calderone | last post by:
On Thu, 9 Oct 2008 06:37:04 -0700 (PDT), WaterWalk <toolmaster@163.comwrote: I disagree. But if you'd rather see the character (or a replacement character, or possibly an empty box, depending on...
3
by: datactrl | last post by:
Hi, all I create a pdf content on fly and then print it out with coding as following. It's used to work fine on an apache server. Now I change to another apache server on different machine, it won't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.