python统计每个年龄的用户各有多少人

Python022

python统计每个年龄的用户各有多少人,第1张

通过调用随机函数获取。Python是一种面向对象、解释型计算机程序设计语言,统计每个年龄的用户通过调用随机函数获取,按照10岁为一个年龄段,比如0~9岁为第一个年龄段,以此类推,统计0~100岁中每个年龄段的人数。

python输⼊出⽣⽇期和当前⽇期计算年龄_python根据出⽣⽇

期计算年龄的代码

python根据出⽣⽇期计算年龄的代码,运⾏后会提醒⽤户输出出⽣的年⽉⽇,然后输出年龄,可以改写为⼀个通⽤函数

from time import *

#a function to find your age

def age():

print "Enter Your Date of Birth"

d=input("Day:")

m=input("Month:")

y=input("Year:")

#get the current time in tuple format

a=gmtime()

#difference in day

dd=a[2]-d

#difference in month

dm=a[1]-m

#difference in year

dy=a[0]-y

#checks if difference in day is negative

if dd<0:

dd=dd+30

dm=dm-1

#checks if difference in month is negative when difference in day is also negative

if dm<0:

dm=dm+12

dy=dy-1

#checks if difference in month is negative when difference in day is positive

if dm<0:

dm=dm+12

dy=dy-1

print "Your current age is %s Years %s Months &%s Days"%(dy,dm,dd)

age()

5.9

百度文库VIP限时优惠现在开通,立享6亿+VIP内容

立即获取

python输入出生日期和当前日期计算年龄_python根据出生日期计算年龄的代码

python输⼊出⽣⽇期和当前⽇期计算年龄_python根据出⽣⽇

期计算年龄的代码

python根据出⽣⽇期计算年龄的代码,运⾏后会提醒⽤户输出出⽣的年⽉⽇,然后输出年龄,可以改写为⼀个通⽤函数

from time import *

#a function to find your age

def age():

print "Enter Your Date of Birth"

第 1 页

d=input("Day:")

m=input("Month:")

y=input("Year:")

#get the current time in tuple format

a=gmtime()

#difference in day

dd=a[2]-d

#difference in month

dm=a[1]-m

使用datetime模块可以直接算出间隔多少天,但是不能按照年,月,日换算。想知道小孩儿多大了都有难度,所以就直接写了一段。

bug:函数没有处理日期大小、日期颠倒,日期大于实际月历等情况,但不影响使用。