python 经典100例(1-20)

'''
【程序1】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去
      掉不满足条件的排列。
2.程序源代码:
'''
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if( i != k ) and (i != j) and (j != k):
print i,j,k

'''
【程序2】
题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高
   于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提
   成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于
   40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于
   100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。      
2.程序源代码:
'''
bonus1 = 100000 * 0.1
bonus2 = bonus1 + 100000 * 0.500075
bonus4 = bonus2 + 200000 * 0.5
bonus6 = bonus4 + 200000 * 0.3
bonus10 = bonus6 + 400000 * 0.15

i = int(raw_input('input gain:\n'))
if i <= 100000:
bonus = i * 0.1
elif i <= 200000:
bonus = bonus1 + (i - 100000) * 0.075
elif i <= 400000:
bonus = bonus2 + (i - 200000) * 0.05
elif i <= 600000:
bonus = bonus4 + (i - 400000) * 0.03
elif i <= 1000000:
bonus = bonus6 + (i - 600000) * 0.015
else:
bonus = bonus10 + (i - 1000000) * 0.01
print 'bonus = ',bonus

'''
【程序3】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后
      的结果满足如下条件,即是结果。请看具体分析:
2.程序源代码:

#include "math.h"
main()
{
long int i,x,y,z;
for (i=1;i<100000;i++)
 { x=sqrt(i+100);   /*x为加上100后开方后的结果*/
  y=sqrt(i+268);   /*y为再加上168后开方后的结果*/
   if(x*x==i+100&&y*y==i+268)/*如果一个数的平方根的平方等于该数,这说明此数是完全平方数*/
    printf("\n%ld\n",i);
 }
}
'''
import math
for i in range(10000):
#转化为整型值
x = int(math.sqrt(i + 100))
y = int(math.sqrt(i + 268))
if(x * x == i + 100) and (y * y == i + 268):
print i

'''题目:输入某年某月某日,判断这一天是这一年的第几天?
1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
      情况,闰年且输入月份大于3时需考虑多加一天。
2.程序源代码:
'''
year = int(raw_input('year:\n'))
month = int(raw_input('month:\n'))
day = int(raw_input('day:\n'))

months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 <= month <= 12:
sum = months[month - 1]
else:
print 'data error'
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print 'it is the %dth day.' % sum


'''
【程序5】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,
      然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
2.程序源代码:
'''
l = []
for i in range(3):
x = int(raw_input('integer:\n'))
l.append(x)
l.sort()
print l

'''
【程序6】--有问题
题目:用*号输出字母C的图案。
1.程序分析:可先用'*'号在纸上写出字母C,再分行输出。
2.程序源代码:
'''
print 'Hello Python world!\n'
print '*' * 10
for i in range(5):
print '* *'
print '*' * 10
print '*\n' * 6

'''
【程序7】
题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
1.程序分析:字符共有256个。不同字符,图形不一样。      
2.程序源代码:
'''
a = 176
b = 219
print chr(b),chr(a),chr(a),chr(a),chr(b)
print chr(a),chr(b),chr(a),chr(b),chr(a)
print chr(a),chr(a),chr(b),chr(a),chr(a)
print chr(a),chr(b),chr(a),chr(b),chr(a)
print chr(b),chr(a),chr(a),chr(a),chr(b)


'''
【程序8】
题目:输出9*9口诀。
1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
2.程序源代码:
#include "stdio.h"
main()
{
 int i,j,result;
 printf("\n");
 for (i=1;i<10;i++)
  { for(j=1;j<10;j++)
    {
     result=i*j;
     printf("%d*%d=%-3d",i,j,result);/*-3d表示左对齐,占3位*/
    }
   printf("\n");/*每一行后换行*/
  }
}
'''
for i in range(1,10):
print( )
for j in range(1,10):
result = i * j
print ('%d * %d = % -2d' % (i,j,result))

'''
【程序9】
题目:要求输出国际象棋棋盘。
1.程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。
2.程序源代码:
#include "stdio.h"
main()
{
int i,j;
for(i=0;i<8;i++)
 {
  for(j=0;j<8;j++)
   if((i+j)%2==0)
    printf("%c%c",219,219);
   else
    printf(" ");
   printf("\n");
 }
}
'''
import sys
for i in range(8):
for j in range(8):
if(i + j) % 2 == 0:
sys.stdout.write(chr(219))
sys.stdout.write(chr(219))
else:
sys.stdout.write(' ')
print ''

'''
【程序10】
题目:打印楼梯,同时在楼梯上方打印两个笑脸。
1.程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。
2.程序源代码:
'''
import sys
sys.stdout.write(chr(1))
sys.stdout.write(chr(1))
print ''

for i in range(1,11):
for j in range(1,i):
sys.stdout.write(chr(219))
sys.stdout.write(chr(219))
print ''

'''
【程序11】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月
   后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
2.程序源代码:
main()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)
 { printf("%12ld %12ld",f1,f2);
   if(i%2==0) printf("\n");/*控制输出,每行四个*/
   f1=f1+f2; /*前两个月加起来赋值给第三个月*/
   f2=f1+f2; /*前两个月加起来赋值给第三个月*/
 }
}
'''
f1 = 1
f2 = 1
for i in range(1,21):
print '%12d %12d' % (f1,f2)
if (i % 2) == 0:
print ''
f1 = f1 + f2
f2 = f1 + f2

def fib(n):
a,b = 1,1
for i in range(n-1):
a,b = b,a+b
return a
# 输出了第10个月兔子出生个数
print( fib(10))

'''
【程序12】
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
      则表明此数不是素数,反之是素数。       
2.程序源代码:
'''
h = 0
leap = 1
from math import sqrt
from sys import stdout
for m in range(101,201):
k = int(sqrt(m + 1))
for i in range(2,k + 1):
if m % i == 0:
leap = 0
break
if leap == 1:
print '%-4d' % m
h += 1
if h % 10 == 0:
print ''
leap = 1
print 'The total is %d' % h

l = []for i in range(101,200):  
for j in range(2,i-1):  
if i%j ==0:  
break  
else:  
l.append(i)
print(l)
print("总数为:%d" % len(l))

'''
【程序13】
题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数
   本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
2.程序源代码:
'''
a = 1
b = 1
c = 1
for a in range(0,10):  
for b in range(0,10):  
for c in range(0,10):  
if int(a)**3+int(b)**3+int(c)**3 == a*100+b*10+c and a != 0:  
x = a*100+b*10+c  
print(x)

'''
【程序14】
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。

程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,
 重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。

2.程序源代码:
'''
from sys import stdout
n = int(raw_input("input number:\n"))
print "n = %d" % n

for i in range(2,n + 1):
while n != i:
if n % i == 0:
stdout.write(str(i))
stdout.write("*")
n = n / i
else:
break
print "%d" % n

'''
【程序15】
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,
   60分以下的用C表示。
1.程序分析:(a>b)?a:b这是条件运算符的基本例子。
2.程序源代码:
不支持这个运算符
'''
score = int(raw_input('input score:\n'))
if score >= 90:
grade = 'A'
elif score >= 60:
grade = 'B'
else:
grade = 'C'

print '%d belongs to %s' % (score,grade)

题目16:输出指定格式的日期。
程序分析:使用 datetime 模块。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import datetime
if __name__ == '__main__':
# 输出今日日期,格式为 dd/mm/yyyy。更多选项可以查看 strftime() 方法
print(datetime.date.today().strftime('%d/%m/%Y'))

# 创建日期对象 miyazakiBirthDate = datetime.date(1941, 1, 5)
print(miyazakiBirthDate.strftime('%d/%m/%Y'))

# 日期算术运算
miyazakiBirthNextDay = miyazakiBirthDate + datetime.timedelta(days=1)print(miyazakiBirthNextDay.strftime('%d/%m/%Y'))

# 日期替换
miyazakiFirstBirthday = miyazakiBirthDate.replace(year=miyazakiBirthDate.year + 1) print(miyazakiFirstBirthday.strftime('%d/%m/%Y'))

'''
【程序17】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
      
2.程序源代码:
'''
import string
s = raw_input('input a string:\n')
letters = 0
space = 0
digit = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print 'char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others)

'''
题目18:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时
   共有5个数相加),几个数相加有键盘控制。
1.程序分析:关键是计算出每一项的值。
2.程序源代码:
'''
Tn = 0
Sn = []
n = int(raw_input('n = :\n'))
a = int(raw_input('a = :\n'))
for count in range(n):
Tn = Tn + a
a = a * 10
Sn.append(Tn)
print Tn

Sn = reduce(lambda x,y : x + y,Sn)
print Sn


'''
【程序19】
题目:一个数如果恰好等于它的因子之和,这个数就称为“完数”。例如6=1+2+3.编程
   找出1000以内的所有完数。
1. 程序分析:请参照程序<--上页程序14.
2.程序源代码:
'''
from sys import stdout
for j in range(2,1001):
k = []
n = -1
s = j
for i in range(1,j):
if j % i == 0:
n += 1
s -= i
k.append(i)
if s == 0:
print j
for i in range(n):
stdout.write(k[i])
stdout.write(' ')
print k[n]

'''
【程序20】
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在
   第10次落地时,共经过多少米?第10次反弹多高?
1.程序分析:见下面注释
2.程序源代码:
'''
Sn = 100.0
Hn = Sn / 2

for n in range(2,11):
Sn += 2 * Hn
Hn /= 2

print( 'Total of road is %f' % Sn)
print( 'The tenth is %f meter' % Hn)

好的,以下是Python海龟作图经典100例: 1. 绘制一个正方形 ```python import turtle turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) ``` 2. 绘制一个圆形 ```python import turtle turtle.circle(50) ``` 3. 绘制一个五角星 ```python import turtle turtle.forward(100) turtle.right(144) turtle.forward(100) turtle.right(144) turtle.forward(100) turtle.right(144) turtle.forward(100) turtle.right(144) turtle.forward(100) ``` 4. 绘制一个正方形螺旋 ```python import turtle for i in range(100): turtle.forward(i) turtle.right(90) ``` 5. 绘制一个彩色螺旋 ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(100): turtle.color(colors[i % 6]) turtle.forward(i) turtle.right(90) ``` 6. 绘制一个三角形 ```python import turtle turtle.forward(100) turtle.left(120) turtle.forward(100) turtle.left(120) turtle.forward(100) ``` 7. 绘制一个五边形 ```python import turtle turtle.forward(100) turtle.right(72) turtle.forward(100) turtle.right(72) turtle.forward(100) turtle.right(72) turtle.forward(100) turtle.right(72) turtle.forward(100) ``` 8. 绘制一个六边形 ```python import turtle turtle.forward(100) turtle.right(60) turtle.forward(100) turtle.right(60) turtle.forward(100) turtle.right(60) turtle.forward(100) turtle.right(60) turtle.forward(100) turtle.right(60) turtle.forward(100) ``` 9. 绘制一个正方形螺旋(使用for循环) ```python import turtle for i in range(100): turtle.forward(i * 2) turtle.right(90) ``` 10. 绘制一个彩色螺旋(使用for循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(100): turtle.color(colors[i % 6]) turtle.forward(i * 2) turtle.right(90) ``` 11. 绘制一个五角星(使用for循环) ```python import turtle for i in range(5): turtle.forward(100) turtle.right(144) ``` 12. 绘制一个正方形(使用for循环) ```python import turtle for i in range(4): turtle.forward(100) turtle.right(90) ``` 13. 绘制一个圆形(使用for循环) ```python import turtle for i in range(360): turtle.forward(1) turtle.right(1) ``` 14. 绘制一个三角形(使用for循环) ```python import turtle for i in range(3): turtle.forward(100) turtle.left(120) ``` 15. 绘制一个六边形(使用for循环) ```python import turtle for i in range(6): turtle.forward(100) turtle.right(60) ``` 16. 绘制一个彩色正方形螺旋(使用for循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(100): turtle.color(colors[i % 6]) turtle.forward(i * 2) turtle.right(90) ``` 17. 绘制一个彩色五角星(使用for循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(5): turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(144) ``` 18. 绘制一个彩色三角形(使用for循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(3): turtle.color(colors[i % 6]) turtle.forward(100) turtle.left(120) ``` 19. 绘制一个彩色六边形(使用for循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(6): turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(60) ``` 20. 绘制一个彩色圆形(使用for循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(360): turtle.color(colors[i % 6]) turtle.forward(1) turtle.right(1) ``` 21. 绘制一个彩色正方形(使用for循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(4): turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(90) ``` 22. 绘制一个彩色正方形螺旋(使用while循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] i = 0 while i < 100: turtle.color(colors[i % 6]) turtle.forward(i * 2) turtle.right(90) i += 1 ``` 23. 绘制一个彩色五角星(使用while循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] i = 0 while i < 5: turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(144) i += 1 ``` 24. 绘制一个彩色三角形(使用while循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] i = 0 while i < 3: turtle.color(colors[i % 6]) turtle.forward(100) turtle.left(120) i += 1 ``` 25. 绘制一个彩色六边形(使用while循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] i = 0 while i < 6: turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(60) i += 1 ``` 26. 绘制一个彩色圆形(使用while循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] i = 0 while i < 360: turtle.color(colors[i % 6]) turtle.forward(1) turtle.right(1) i += 1 ``` 27. 绘制一个彩色正方形(使用while循环) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] i = 0 while i < 4: turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(90) i += 1 ``` 28. 绘制一个正方形螺旋(使用函数) ```python import turtle def square_spiral(): for i in range(100): turtle.forward(i * 2) turtle.right(90) square_spiral() ``` 29. 绘制一个彩色正方形螺旋(使用函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_square_spiral(): for i in range(100): turtle.color(colors[i % 6]) turtle.forward(i * 2) turtle.right(90) color_square_spiral() ``` 30. 绘制一个彩色五角星(使用函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_star(): for i in range(5): turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(144) color_star() ``` 31. 绘制一个彩色三角形(使用函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_triangle(): for i in range(3): turtle.color(colors[i % 6]) turtle.forward(100) turtle.left(120) color_triangle() ``` 32. 绘制一个彩色六边形(使用函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_hexagon(): for i in range(6): turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(60) color_hexagon() ``` 33. 绘制一个彩色圆形(使用函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_circle(): for i in range(360): turtle.color(colors[i % 6]) turtle.forward(1) turtle.right(1) color_circle() ``` 34. 绘制一个彩色正方形(使用函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_square(): for i in range(4): turtle.color(colors[i % 6]) turtle.forward(100) turtle.right(90) color_square() ``` 35. 绘制一个彩色正方形螺旋(使用递归) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_square_spiral_recursive(length): if length > 0: turtle.color(colors[length % 6]) turtle.forward(length * 2) turtle.right(90) color_square_spiral_recursive(length - 1) color_square_spiral_recursive(99) ``` 36. 绘制一个彩色五角星(使用递归) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_star_recursive(length): if length > 0: turtle.color(colors[length % 6]) turtle.forward(length) turtle.right(144) color_star_recursive(length - 1) color_star_recursive(100) ``` 37. 绘制一个彩色三角形(使用递归) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_triangle_recursive(length): if length > 0: turtle.color(colors[length % 6]) turtle.forward(length) turtle.left(120) color_triangle_recursive(length - 1) color_triangle_recursive(100) ``` 38. 绘制一个彩色六边形(使用递归) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_hexagon_recursive(length): if length > 0: turtle.color(colors[length % 6]) turtle.forward(length) turtle.right(60) color_hexagon_recursive(length - 1) color_hexagon_recursive(100) ``` 39. 绘制一个彩色圆形(使用递归) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_circle_recursive(length): if length > 0: turtle.color(colors[length % 6]) turtle.forward(1) turtle.right(1) color_circle_recursive(length - 1) color_circle_recursive(360) ``` 40. 绘制一个彩色正方形(使用递归) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] def color_square_recursive(length): if length > 0: turtle.color(colors[length % 6]) turtle.forward(length) turtle.right(90) color_square_recursive(length - 1) color_square_recursive(100) ``` 41. 绘制一个正方形螺旋(使用列表推导式) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] [length for length in range(0, 200, 2) for color in [colors[length // 2 % 6]] for _ in range(2) for _ in [turtle.color(color), turtle.forward(length), turtle.right(90)] ] ``` 42. 绘制一个彩色正方形螺旋(使用列表推导式) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] [(length, color) for length in range(0, 200, 2) for color in [colors[length // 2 % 6]] ] for length, color in _: turtle.color(color) turtle.forward(length) turtle.right(90) ``` 43. 绘制一个彩色五角星(使用列表推导式) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] [(100, colors[i % 6]) for i in range(5)] for length, color in _: turtle.color(color) turtle.forward(length) turtle.right(144) ``` 44. 绘制一个彩色三角形(使用列表推导式) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] [(100, colors[i % 6]) for i in range(3)] for length, color in _: turtle.color(color) turtle.forward(length) turtle.left(120) ``` 45. 绘制一个彩色六边形(使用列表推导式) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] [(100, colors[i % 6]) for i in range(6)] for length, color in _: turtle.color(color) turtle.forward(length) turtle.right(60) ``` 46. 绘制一个彩色圆形(使用列表推导式) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] [(1, colors[i % 6]) for i in range(360)] for length, color in _: turtle.color(color) turtle.forward(length) turtle.right(1) ``` 47. 绘制一个彩色正方形(使用列表推导式) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] [(100, colors[i % 6]) for i in range(4)] for length, color in _: turtle.color(color) turtle.forward(length) turtle.right(90) ``` 48. 绘制一个正方形螺旋(使用lambda函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] square_spiral = lambda i: turtle.color(colors[i % 6]), turtle.forward(i * 2), turtle.right(90) [square_spiral(i) for i in range(100)] ``` 49. 绘制一个彩色正方形螺旋(使用lambda函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] color_square_spiral = lambda i: turtle.color(colors[i % 6]), turtle.forward(i * 2), turtle.right(90) [color_square_spiral(i) for i in range(100)] ``` 50. 绘制一个彩色五角星(使用lambda函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] color_star = lambda i: turtle.color(colors[i % 6]), turtle.forward(100), turtle.right(144) [color_star(i) for i in range(5)] ``` 51. 绘制一个彩色三角形(使用lambda函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] color_triangle = lambda i: turtle.color(colors[i % 6]), turtle.forward(100), turtle.left(120) [color_triangle(i) for i in range(3)] ``` 52. 绘制一个彩色六边形(使用lambda函数) ```python import turtle colors = ["red", "orange", "yellow", "green", "blue", "purple"] color_hexagon = lambda i: turtle.color(colors[i % 6]), turtle.forward(100), turtle.right(60) [color_hex
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值