1. 程式人生 > >Python小例子(一)

Python小例子(一)

1.根據半徑r計算圓半徑

r=25
area=3.1415*r*r
print(area)
print("{:.2f}".format(area))

2.繪製同心圓

import turtle
turtle.pensize(2)
turtle.circle(10)
turtle.circle(40)
turtle.circle(80)
turtle.circle(160)

3.溫度轉換

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("輸入的格式錯誤")

4、繪製五角星

from turtle import *
color('red','red')
begin_fill()
for i in range(5):
    fd(200)
    rt(144)
end_fill()
done()