Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old February 22nd, 2004, 07:23 PM
jfucile jfucile is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 jfucile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking Trying to create an age calculator...

I have a couple problems with this program.

I am using a tuple that contains all of the months and want to be able to get the user to re-enter the data if they enter in invalid month, day or year ? Here is the code I have so far:

months = ("january", "february", "march", "april", "may", "june", "july",
"august", "september", "october", "november", "december")

while True:
bmonth = raw_input("Please enter your Birth Month: ")
bmonth = bmonth.lower()
for mymonth in months:
if mymonth == bmonth:
print "you have entered a valid month"
else:
print "you need to print a valid month"

raw_input("\n\nPress the enter key to exit.")



Also I cant seem to get a formula to work with integers that will calculate the person's age any help I can get would be appreciated

Reply With Quote
  #2  
Old February 22nd, 2004, 09:07 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
Those lines
Code:
for mymonth in months:
   if mymonth == bmonth:
could be replaced by
Code:
if mymonth in bmonth:


But as for calculating a person's age, do you need just the number of years ? Or do you also want months, days ?
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz

Reply With Quote
  #3  
Old February 22nd, 2004, 10:49 PM
jfucile jfucile is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 jfucile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by SolarBear
Those lines
Code:
for mymonth in months:
   if mymonth == bmonth:
could be replaced by
Code:
if mymonth in bmonth:


But as for calculating a person's age, do you need just the number of years ? Or do you also want months, days ?



I want to calculate age in years using users birth month, day and year compared to current month, day and year.

thank you for the help on the first part....

Reply With Quote
  #4  
Old February 22nd, 2004, 11:23 PM
Boceifus's Avatar
Boceifus Boceifus is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 93 Boceifus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 Days 2 h 5 m 27 sec
Reputation Power: 5
Quote:
Originally Posted by jfucile
I want to calculate age in years using users birth month, day and year compared to current month, day and year.



this isnt the most efficant way,but its something to give ya an idea:

Code:
import time

date=time.ctime()
cmonth=date[4:7]  #current month
cday=int(date[8:10])  #current day
cyear=int(date[20:24]) #current year

month=raw_input('month?')
day=int(raw_input('day?'))
year=int(raw_input('year?'))


if month == 'January':
    month=int('01')
elif month == 'Febuary':
    month=int('02')
elif month == 'March':
    month=int('03')
#etc etc etc
    
if cmonth == 'Jan':
    cmonth=int('01')
elif cmonth == 'Feb':
    cmonth=int('02')
elif cmonth == 'Mar':
    cmonth=int('03')
#etc etc etc

age = cyear-year 
    
if month > cmonth: 
    print age-1
elif month < cmonth:
    print age
else:
    if day < cday:
        print age
    elif day > cday:
        print age-1
    else:
        print 'Happy Birthday!'
        print age
__________________
It is not important if the glass is half full or half empty.What is important,is who has been drinking from MY glass?!?!?

Last edited by Boceifus : February 23rd, 2004 at 07:34 AM. Reason: edited values for cmonth(only reads 3 chars),shortened

Reply With Quote
  #5  
Old February 23rd, 2004, 07:24 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Not a problem at all, the time module provides everything we need and the math is very easy so here goes .

Code:
#!/usr/bin/env python

import time

while True:
    
    print 'Please enter dates in the following format - DD/MM/YYYY\n'
    
    birth =  raw_input('Date of Birth: ')
    
    try:
        born = time.strptime(birth, '%d/%m/%Y')[:3]
        date = time.localtime()[:3]
        print int(float('%d.%d%d' % (date)) - float('%d.%d%d' % (born))), '\n'
    except:
        print 'An error occured while processing your entry...\n'


I wasn't really sure how acurate you wanted this (to the day) but the program will return an int of the users age in years acurate up the the their birthday i.e. 18.

You can change the input format to anything you want, see the time module for more info on that:

http://www.python.org/doc/2.3.3/lib/module-time.html

Note: this is untested so if it doesn't work let me know, although i see no reason why it shouldn't .

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #6  
Old February 23rd, 2004, 07:49 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Chief brought it to my attention that my script brakes when the day and or month is a two digit int i.e 10, 11, 12.

Luckily this can easily be fixed by padding each number using string formatting .

Code:
#!/usr/bin/env python

import time

while True:
    
    print 'Please enter dates in the following format - DD/MM/YYYY\n'
    
    birth =  raw_input('Date of Birth: ')
    
    try:
        born = time.strptime(birth, '%d/%m/%Y')[:3]
        date = time.localtime()[:3]
        print int(float('%04d.%02d%02d' % (date)) - float('%04d.%02d%02d' % (born))), '\n'
    except:
        print 'An error occured while processing your entry...\n'


Thanks again Chief,

Mark.

Reply With Quote
  #7  
Old February 24th, 2004, 10:44 AM
jfucile jfucile is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 jfucile User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I attempted to run the code that was supplied but it keeps erroring out.


I gues it may have to do with me runing this on a windows box, I may have to have the user enter in all the variables manually and then change them to an integer after combining them.

Any suggestions ?

Reply With Quote
  #8  
Old February 24th, 2004, 01:09 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Mmmm no, there shouldn't be a problem with either since Chief and i both have windows boxes and we've both been messing around with it without any problems. Whats the error?

Note: if you're not using Python 2.3 then you'll need to change 'True' to '1'.

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Trying to create an age calculator...


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway