python习题练习(四)

参考书籍:python程序设计

chapter6.

test

#happy2.py
#This pro is a exercise in the book.
def happy():
    return "Happy Birthday to you!\n"

def verseFor(person):
    lyrics = happy() * 2 + "Happy birthday, dear " + person + ".\n" + happy()
    return lyrics

def main():
    for person in ['Fred', 'Lucy', 'Elmer']:
        print(verseFor(person))

main()

4.

#This pro calculates the sum and Cube sum of n nums.
#input: nature number n
#output: sum and Cube sum of n.
def sumN(n):
    s = 0
    for i in range(1, n + 1):
        s += i
    return s

def sumNCubes(n):
    s = 0
    for i in range(1, n + 1):
        i = i ** 3
        s += i
    return s 

def main():
    num = int(input("Enter a natural num: "))
    print("The sum are {0} and {1}.".format(sumN(num), sumNCubes(num)))

main()

13.

#This pro convert a string list to a float list.
def toNumbers(lst):
    #Notice: The func has no return.
    for i in range(len(lst)):
        lst[i] = float(lst[i])

def main():
    lit = ['12', '1.2', '84', '2.4', '1.258']
    toNumbers(lit)
    print(lit)

main()

14.

#This pro cals the SquareSum of the nums read from a file.
#input: filename
#output: SquareSum
def SquareSum(lit):
    #square sum of a num list
    s = 0
    for i in lit:
        i = i ** 2
        s += i
    return s

def toNumbers(lst):
    #convert a str list to a float list
    #Notice: The func has no return.
    for i in range(len(lst)):
        lst[i] = float(lst[i])

def main():
    filename = input("Enter the filename:")
    infile = open(filename, 'r')
    lst = infile.readlines()
    toNumbers(lst)
    print(SquareSum(lst))
    infile.close()

main()

15.

#This pro draws some simple faces in one win.
from graphics import *
def drawCircle(center, size):
    myCircle = Circle(center, size)
    myCircle.setOutline('black')
    myCircle.setWidth(1)
    return myCircle

def drawFace(center, size, win):
    #face
    Face = drawCircle(center, size)
    Face.draw(win)
    #left eye
    leftcenter = Point(center.getX() - size / 2.8, center.getY() + size / 2.8)
    Lefteye = drawCircle(leftcenter, size / 10)
    Lefteye.draw(win)
    #right eye
    righteye = Lefteye.clone()
    righteye.move(size / 1.4, 0)
    righteye.draw(win)
    #mouth
    mouthcenter = Point(center.getX(), center.getY() - size / 2.8)
    mouth = drawCircle(mouthcenter, size / 12)
    mouth.draw(win)
    #nose
    nose = drawCircle(center, size / 20)
    nose.setFill('black')
    nose.draw(win)

def main():
    win = GraphWin("Faces", 400, 400)
    win.setBackground('white')
    win.setCoords(0, 0, 100, 100)
    #click to draw
    win.getMouse()
    #face1
    drawFace(Point(10, 10), 9, win)
    #face2
    drawFace(Point(40, 40), 15, win)
    #face3
    drawFace(Point(70, 70), 26, win)
    #click to quit
    win.getMouse()

main()

16.

#This pro draws some simple faces in one picture.
#input: filename of the pic. num of faces.
#output: some faces in the pic.
from graphics import *
import math

def drawCircle(center, size):
    myCircle = Circle(center, size)
    myCircle.setOutline('black')
    myCircle.setWidth(1)
    return myCircle

def drawWin(ldx, ldy, rux, ruy):
    win = GraphWin("Faces", 400, 400)
    win.setBackground('white')
    win.setCoords(ldx, ldy, rux, ruy)
    return win

def drawFace(center, size, win):
    #face
    Face = drawCircle(center, size)
    Face.draw(win)
    #left eye
    leftcenter = Point(center.getX() - size / 2.8, center.getY() + size / 2.8)
    Lefteye = drawCircle(leftcenter, size / 10)
    Lefteye.draw(win)
    #right eye
    righteye = Lefteye.clone()
    righteye.move(size / 1.4, 0)
    righteye.draw(win)
    #mouth
    mouthcenter = Point(center.getX(), center.getY() - size / 2.8)
    mouth = drawCircle(mouthcenter, size / 12)
    mouth.draw(win)
    #nose
    nose = drawCircle(center, size / 20)
    nose.setFill('black')
    nose.draw(win)

def locate(p1, p2):
    size = math.sqrt((p1.getX() - p2.getX()) ** 2 + (p1.getY() - p2.getY()) ** 2)
    return p1, size

def main():
    #draw the window
    win = drawWin(0, 0, 100, 100)
    #draw the pic
    filename = input("Enter the filename:")
    image = Image(Point(50, 50), filename)
    image.draw(win)
    #how many feces
    n = int(input("Enter a number:"))
    #draw faces
    for i in range(n):
        #click the center
        p1 = win.getMouse()
        #click the side
        p2 = win.getMouse()
        #locate
        center, size = locate(p1, p2)
        #draw
        drawFace(center, size, win)
    #click to quit
    win.getMouse()

main()

17.

#This pro move the circle to the click zone 10 times.

from graphics import *

def drawCircle(center, size):
    myCircle = Circle(center, size)
    myCircle.setOutline('black')
    myCircle.setWidth(1)
    return myCircle

def drawWin():
    win = GraphWin("Moving", 400, 400)
    win.setBackground('white')
    win.setCoords(0, 0, 100, 100)
    return win

def moveTo(shape, newCenter):
    center = shape.getCenter()
    dx = newCenter.getX() - center.getX()
    dy = newCenter.getY() - center.getY()
    shape.move(dx, dy)

def main():
    #draw the win
    win = drawWin()
    #draw the circle
    circle = drawCircle(Point(50, 50), 10)
    circle.draw(win)
    #move the circle
    for i in range(10):
        p = win.getMouse()
        moveTo(circle, p)
    #click to quit
    print("Click to quit.")
    win.getMouse()
    win.close()

main()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值