一、计算圆的面积
- 思路:根据圆面积的计算公式进行求解。
- 程序代码:
1 import math 2 radius = 25 3 area = math.pi*radius**2 #**是幂运算 4 print(area) 5 print(':.2f'.format(area))
二、摄氏温度与华氏温度相互转换
1.思路:
1).利用温度转换公式,其中,f代表华氏温度,c代表摄氏温度;
2).根据输入的格式自动识别待转换的温度类型;
3).如果输入格式错误,则给出相应提示。
2.程序代码:
#temp_convert.py tempStr = input('请输入带有符号的温度值:') if tempStr[-1] in ['F','f']: c = (eval(tempStr[0:-1])-32)/1.8 print('转换后的温度是{:.2f}C'.format(c)) elif tempStr[-1] in ['C','c']: f = 1.8*eval(tempStr[0,-1])+32 print('转换后的温度是{:.2f}F'.format(f)) else: print("输入格式错误")
三、绘制五角星
1.思路:调用海龟标准绘图模块。
2.程序代码:
1 from turtle import * 2 fillcolor("red") 3 begin_fill() 4 while True: 5 forward(200) 6 right(144) 7 if ads(pos())<1: 8 break 9 end_fill()
3.效果图: