一,利用turtle库画一串糖葫芦。
import turtle as t
t.pensize(5)
t.hideturtle()
t.pu()
t.goto(-255, 0)
t.down()
t.fd(100)
t.pu()
t.goto(-120, -30)
t.color("red")
t.begin_fill()
for i in range(5):
t.pd()
t.circle(30)
t.pu()
t.fd(60)
t.end_fill()
t.color("black")
t.goto(155, 0)
t.pd()
t.fd(100)
t.done()
import turtle as t
c = ["red", "pink", "blue"]
ra = [20, 30, 50]
pi = 3.14159
for x in range(3):
t.pu()
t.goto(0, -ra[x])
t.pd()
t.color(c[x])
t.circle(ra[x])
t.done
三,将扑克牌洗牌后发给四个玩家(不考虑大王和小王),分别输出各玩家手中的牌型。
from random import shuffle
nums = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
types = ['♥', '♣', '♦', '♠']
poker = []
for i in types:
for j in nums:
poker.append((i, j))
shuffle(poker)
A = []
B = []
C = []
D = []
for n in range(len(poker)):
if (n % 4 == 0):
A.append(poker[n])
elif (n % 4 == 1):
B.append(poker[n])
elif (n % 4 == 2):
C.append(poker[n])
else:
D.append(poker[n])
print("A的牌是" + A.__repr__())
print("B的牌是" + B.__repr__())
print("C的牌是" + C.__repr__())
print("D的牌是" + D.__repr__())
四,猜数游戏。[20,30]中随机产生一个整数,用户通过键盘输入所猜的数,如果大于预设的数,显示“太大了”;小于预设的数,显示“太小了”,如此循环,直至猜中该数。
import random
n = random.randint(20, 30)
while (True):
a = input("请输入一个整数")
if (int(a) < n):
print("太小了")
elif (int(a) > n):
print("太大了")
elif (int(a) == n):
print("恭喜你猜对了,n是%d" % n)
break
五,用户输入一个整数n,如果n是奇数,则输出1+1/3+1/5+…1/n。如果n是偶数,则输出1/2+1/4+…1/n。
def f(n):
sum = 0
if n % 2 == 1:
for i in range(1, n + 1, 2):
sum += 1 / i
else:
for i in range(2, n + 1, 2):
sum += 1 / i
return sum
n = int(input())
print("%.2f" % f(n))
poem = '''
When you are old and grey and full of sleep,And nodding by the fire,take down \
this book,And slowly read,and dream of the soft look Your eyes had once,and of their \
shadows deep;How many loved your moments of glad grace,And loved your beauty with love \
false or true,But one man loved the pilgrim Soul in you And loved the sorrows of your \
changing face;And bending down beside the glowing bars,Murmur,a little sadly,how Love \
fled And paced upon the mountains overhead And hid his face amid a crowd of stars.
'''
poem = poem.replace(',',' ').strip()
poem = poem.replace(';',' ').strip()
poem = poem.replace('.',' ').strip()
words = poem.split(' ')
word = list(set(words))
word.sort(key=words.index)
dt = {}
for i in word:
dt[i] = words.count(i)
print("单词出现的次数统计为:")
print(dt)
career = input("请输入同学的就业行业名称")
cas = career.split(" ")
ca = list(set(cas))
d = {}
for i in ca:
d[i] = cas.count(i)
for k in sorted(d, key=d.__getitem__, reverse=True):
print("%s:%d" % (k, d[k]))