python函数例题

  • Post author:
  • Post category:python


1.计算圆面积:

from math import pi as PI
def CircleArea(r):
    if isinstance(r,(int,float) and r>0:
        return PI*r*r
    else:
        return('You must give me an integer or float as radius.')
print(CircleArea(3))

2.模拟轮盘抽奖游戏:一等奖、二等奖、三等奖。轮盘随机转动:一等奖范围[0, 0.08];二等奖范围[0.08, 0.3];三等奖范围[0.3, 1]。模拟本次活动1000人参加,模拟游戏需要准备个等级评奖的个数。

def demo(s):
    zimugeshu=[0,0]
    for ch in s:
        if 'A'<= ch <='Z':
            zimugeshu[0]+= 1
        if 'a'<= ch <='z':
            zimugeshu[1]+= 1
    return tuple(zimugeshu)
print(demo('abcDFFdeg'))

#2
import random
def main(s):
dict={'一等奖':(0,0.08),'二等奖':(0.08,0.3),'三等奖':(0.3,1)}
result=dict{}
n=random()
for k,v in dict_1.items():
    if v[0]<= n <=v[1]:
        return k
for i in range(1000):
    i=prize(dict_1)
    result[i]=result.get(i,0)+1
for j in result.items():
    print(j)

3.编写函数,接收字符串参数,返回一个元组,其中第一个元素为大写字母个数,第二个元素为小写字母个数。

def demo(s):
	result=[0,0]
	for ch in s:
		if 'A'<=ch<='Z':
			result[0]+=1
		elif 'a'<=ch<='z':
			result[1]+=1
	return tuple(result)
print(demo('abcADBssss'))



版权声明:本文为wxx20原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。