Python语言程序设计基础(2)—— Python程序实例解析
生活随笔
收集整理的這篇文章主要介紹了
Python语言程序设计基础(2)—— Python程序实例解析
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
溫度轉(zhuǎn)換
def tempConvert(ValueStr):if ValueStr[-1] in ['F','f']:ans = (eval(ValueStr[0:-1]) - 32)/1.8print("{:.2f}C".format(ans))elif ValueStr[-1]=='C' or ValueStr[-1]=='c':ans = 1.8*eval(ValueStr[0:-1]) + 32print("{:.2f}F".format(ans))else:print("格式錯誤")str = input() tempConvert(str)?
畫蟒蛇
import turtledef drawSnake(radius,angle,length):turtle.seth(-angle/2)for i in range(length):turtle.circle(radius,angle)turtle.circle(-radius,angle)turtle.circle(radius,angle/2)turtle.fd(40)turtle.circle(16,180)turtle.fd(40*2/3) turtle.setup(0.5,0.5) turtle.penup() turtle.fd(-250) turtle.pendown() turtle.pensize(25) turtle.pencolor("purple") drawSnake(40,120,4) turtle.done()?
習(xí)題部分
溫度轉(zhuǎn)換,輸出整數(shù) def tempConvert(ValueStr):print(ValueStr)if ValueStr[-1] in ['F','f']:ans = (eval(ValueStr[0:-1]) - 32)/1.8print("{:.0f}C".format(ans))elif ValueStr[-1]=='C' or ValueStr[-1]=='c':ans = 1.8*eval(ValueStr[0:-1]) + 32print("{:.0f}F".format(ans))else:print("格式錯誤")tempConvert(eval(input()))?
美元轉(zhuǎn)換 def calc(x):if x[-1]=='$':return eval(x[0:-1])*6elif x[-1]=='¥':return eval(x[0:-1])/6else :return -1 money = input() print(calc(money),end='') if money[-1]=='$':print('¥') elif money[-1]=='¥':print('$') else :print("errer")?
彩色蛇 import turtledef drawSnake(radius,angle,length):turtle.seth(-angle/2)color = ["grey","gold","violet","purple","black"]for i in range(length):turtle.pencolor(color[i])turtle.circle(radius,angle)turtle.circle(-radius,angle)turtle.pencolor(color[-1])turtle.circle(radius,angle/2)turtle.fd(40)turtle.circle(16,180)turtle.fd(40*2/3)turtle.setup(0.5,0.5) turtle.penup() turtle.fd(-250) turtle.pendown() turtle.pensize(25) #turtle.pencolor("purple") drawSnake(40,120,4) turtle.done()?
三角形 import turtle import math def paintS():turtle.setup(0.5,0.5)for i in range(3):turtle.seth(120*i)turtle.forward(50)turtle.done()?
四邊形 def paintX():turtle.setup(0.5,0.5)turtle.pensize(5)turtle.pencolor("gold")for i in range(4):turtle.seth(i*90)turtle.penup()turtle.forward(20)turtle.pendown()turtle.forward(60)turtle.penup()turtle.forward(20)turtle.done() paintX()?
六角形 import turtle import mathdef paint():turtle.pensize(5)x = -30turtle.seth(x)for i in range(1,4):turtle.forward(150)turtle.seth(x+120)x+=120turtle.penup()turtle.seth(-60)turtle.forward(50*math.sqrt(3))turtle.pendown()x = 30turtle.seth(x)for i in range(1,4):turtle.forward(150)turtle.seth(x+120)x+=120turtle.done() paint()?
正方形螺旋線 import turtle import mathdef painZ():turtle.pensize(2)cnt = 0x = 10for i in range(1,60):turtle.seth(90*i)if cnt >=2 :x+=10cnt = 0turtle.forward(x)cnt+=1turtle.done()painZ()?
轉(zhuǎn)載于:https://www.cnblogs.com/TreeDream/p/9782282.html
總結(jié)
以上是生活随笔為你收集整理的Python语言程序设计基础(2)—— Python程序实例解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MariaDB数据库日志
- 下一篇: 最大堆