python:女生节礼物,怎么用代码写一个玫瑰+

Python021

python:女生节礼物,怎么用代码写一个玫瑰+,第1张

import turtle

import time

turtle.speed(1)

# 设置初始位置

turtle.penup()

turtle.left(90)

turtle.fd(200)

turtle.pendown()

turtle.right(90)

# 花蕊

turtle.fillcolor("red")

turtle.begin_fill()

turtle.circle(10, 180)

turtle.circle(25, 110)

turtle.left(50)

turtle.circle(60, 45)

turtle.circle(20, 170)

turtle.right(24)

turtle.fd(30)

turtle.left(10)

turtle.circle(30, 110)

turtle.fd(20)

turtle.left(40)

turtle.circle(90, 70)

turtle.circle(30, 150)

turtle.right(30)

turtle.fd(15)

turtle.circle(80, 90)

turtle.left(15)

turtle.fd(45)

turtle.right(165)

turtle.fd(20)

turtle.left(155)

turtle.circle(150, 80)

turtle.left(50)

turtle.circle(150, 90)

turtle.end_fill()

# 花瓣1

turtle.left(150)

turtle.circle(-90, 70)

turtle.left(20)

turtle.circle(75, 105)

turtle.setheading(60)

turtle.circle(80, 98)

turtle.circle(-90, 40)

# 花瓣2

turtle.left(180)

turtle.circle(90, 40)

turtle.circle(-80, 98)

turtle.setheading(-83)

# 叶子1

turtle.fd(30)

turtle.left(90)

turtle.fd(25)

turtle.left(45)

turtle.fillcolor("green")

turtle.begin_fill()

turtle.circle(-80, 90)

turtle.right(90)

turtle.circle(-80, 90)

turtle.end_fill()

turtle.right(135)

turtle.fd(60)

turtle.left(180)

turtle.fd(85)

turtle.left(90)

turtle.fd(80)

# 叶子2

turtle.right(90)

turtle.right(45)

turtle.fillcolor("green")

turtle.begin_fill()

turtle.circle(80, 90)

turtle.left(90)

turtle.circle(80, 90)

turtle.end_fill()

turtle.left(135)

turtle.fd(60)

turtle.left(180)

turtle.fd(60)

turtle.right(90)

turtle.circle(200, 60)

turtle.done()

16年年会抽奖网上有人对公司的抽奖结果又偏见,于是全员进行了抽奖代码的review,好像是爱奇艺公司的,下面用python来实现一个抽奖程序。

主要功能有

1.从一个csv文件中读入所有员工工号

2.将这些工号初始到一个列表中

3.用random模块下的choice函数来随机选择列表中的一个工号

4.抽到的奖项的工号要从列表中进行删除,以免再次抽到

初级版

这个比较简单,缺少定制性,如没法设置一等奖有几名,二等奖有几名

import csv#创建一个员工列表emplist = []#用with自动关闭文件with open('c://emps.csv') as f:

empf = csv.reader(f)for emp in empf:

emplist.append(emp)

print("进行一等奖抽奖,共有一名")import random#利用random模块的chice函数来从列表中随机选取一个元素e1 = random.choice(emplist)#将中奖的员工从列表中剔除emplist.remove(e1)

print('一等奖得主的号码是 %s' % e1)

print('进行三个二等奖的号码抽奖')

e2_1 = random.choice(emplist)

emplist.remove(e2_1)

e2_2 = random.choice(emplist)

emplist.remove(e2_2)

e2_3 = random.choice(emplist)

emplist.remove(e2_3)

print('获得3个二等奖是 %s %s %s',(e2_1,e2_2,e2_3))#下面依次类推可以设置三等奖的抽奖123456789101112131415161718192021222324

改进版

上面的那个初级版,假如要设置个三等奖一百名那么将要重新维护几百行代码,下面用比较高级点的办法实现.

我们考虑用面向对象来实现,设计一个抽奖类,类中包含一个属性(号码来源),一个方法:产生所有抽奖层次指定个数的抽奖号码。

用到如下知识点:

1. csv模块部分函数用法

2. sys模块读取输入

3. random模块函数choice函数用法

4. 列表和字典元素的添加、删除

6. for循环中range用法

7. 类和面向对象

8. 字符打印,print中的计算

9.open中with

#!/usr/bin/python#coding=utf-8import csvimport sysimport random

reload(sys)

sys.setdefaultencoding('utf8')#coding=utf-8print("开始进行抽奖")#定义个抽奖类,功能有输入抽奖级别和个数,打印出每个级别的抽奖员工号码class Choujiang:

#定义scv文件路径

def __init__(self,filepath):

self.empfile = filepathdef creat_num(self):

emplist = []with open(self.empfile) as f:

empf = csv.reader(f)for emp in empf:

emplist.append(emp)

print('共有%s 人参与抽奖' % len(emplist))

levels = int(input('抽奖分几个层次,请输入:'))#定义一个字典

level_dict = {}for i in range(0,levels):

print('请输入当前获奖层次 %s 对应的奖品个数' % ( i + 1))

str_level_dict_key = sys.stdin.readline()

int_level_dict_key = int(str_level_dict_key)

level_dict[i] = int_level_dict_key#循环完成后抽奖层次字典构造完毕

#进行抽奖开始

print('抽奖字典设置为: %s' % level_dict)for i in range(0,len(level_dict)):

winers = []#产生当前抽奖层次i对应的抽奖个数

for j in range(0,int(level_dict[i])):#利用random模块中的choice函数从列表中随机产生一个

winer = random.choice(emplist)

winers.append(winer)

emplist.remove(winer)

print('抽奖层次 %s 下产出的获奖人员有:' % (i + 1 ))

print(winers)#类功能定义完毕,开始初始化并使用if __name__ == '__main__':

peoples = Choujiang('c://emps.csv')

peoples.creat_num()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647

该段程序在python 2.6 以上及 3中均可以运行,运行结果如下图:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32

Type "copyright", "credits" or "license()" for more information.>>>================================ RESTART ================================>>>开始进行抽奖

共有24790 人参与抽奖

抽奖分几个层次,请输入:2请输入当前获奖层次 1 对应的奖品个数1请输入当前获奖层次 2 对应的奖品个数3抽奖字典设置为: {0: 1, 1: 3}

抽奖层次 1 下产出的获奖人员有:

[['张三19826']]

抽奖层次 2 下产出的获奖人员有:

[['张三18670'], ['张三23235'], ['张三15705']]>>>1234567891011121314151617