简单的Python小程序

来自CloudWiki
112.230.96.59讨论2020年2月21日 (五) 01:48的版本 例题
跳转至: 导航搜索

排错

  • P1-8.png
  • 碰到错误提示不要慌,耐心百度一下发生错误的关键词。

例题

斐波那契数列的计算

 
a,b = 0,1
while a<1000:
    print(a,end=',')
    a,b = b,a+b

4)圆面积的计算:

r=25
area=3.1415*r*r
print(area)
print("{:.2f}".format(area))#只输出两位小数

5)绘制五角红星

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

绘制七彩圆圈:

import turtle
colors = ['red','orange','yellow','green','blue','indigo','purple']

for i in range(7):
    c=colors[i]
    turtle.color(c,c)
    turtle.begin_fill()
    turtle.rt(360/7)
    turtle.circle(50)
    turtle.end_fill()

turtle.done()


  • Python小程序之圆面积的计算
  • 根据圆的半径求出圆的面积
radius = 25 # 圆的半径是25
area = 3.1415 * radius * radius # 输入计算圆面积的公式
print(area)
print("{:.2f}".format(area)) # 只输出两位小数
  • Python小程序之人名对话
  • 获得字符串的全部或部分字符
name = input("输入姓名:")
print("%s 同学,学好Python,前途无量!"%name)
print("%s 大侠,学好Python,大展拳脚!"%name[0])
print("%s 哥哥,学好Python,人见人爱!"%name[1:])
  • Python小程序之同切圆的绘制
import turtle # 引用turtle 库
turtle.pensize(2) # 设置画笔宽度为2 像素
turtle.circle(10) # 绘制半径为10 像素的圆
turtle.circle(40) # 绘制半径为40 像素的圆
turtle.circle(80) # 绘制半径为80 像素的圆
turtle.circle(160) # 绘制半径为160 像素的圆

下一章 第2章 Python程序实例解析