python习题练习(二)

参考书籍:python程序设计

chapter4

1.

from graphics import *

def main():
    win = GraphWin()
    shape = Rectangle(Point(10,10), Point(20, 20))
    shape.setOutline('green')
    shape.setFill('blue')
    shape.draw(win)
    for i in range(10):
        p = win.getMouse()
        c = shape.getCenter()
        dx = p.getX() - c.getX()
        dy = p.getY() - c.getY()
        other = shape.clone()
        other.move(dx, dy)
        other.draw(win)
    print('click again to quit!')
    win.getMouse()
    win.close()

main()

6.

from graphics import *

def main():
    print('This program plots the growth of a 10-year investment.')

    # draw window
    win = GraphWin('Investment Growth Chart', 320, 240)
    win.setBackground('white')
    win.setCoords(-1.75, -200, 11.5, 10400)


    # draw labels
    Text(Point(-1, 0), ' 0.0k').draw(win)
    Text(Point(-1, 2500), ' 2.5k').draw(win)
    Text(Point(-1, 5000), ' 5.0k').draw(win)
    Text(Point(-1, 7500), ' 7.5k').draw(win)
    Text(Point(-1, 10000), '10.0k').draw(win)
    
    # draw princioals and apr
    Text(Point(2, 9000), 'principal:').draw(win)
    Text(Point(2, 8000), 'APR:').draw(win)
    pri = Entry(Point(6, 9000), 5)
    apr = Entry(Point(6, 8000), 5)
    pri.setText("0.0")
    apr.setText("0.0")
    pri.draw(win)
    apr.draw(win)
    
    # wait for click
    win.getMouse()

    # get pri and apr, draw the bar in 0 year
    pri_f = float(pri.getText())
    apr_f = float(apr.getText())
    bar = Rectangle(Point(0, 0), Point(1, pri_f))
    bar.setFill('green')
    bar.setOutline('blue')
    bar.setWidth(2)
    bar.draw(win)

    for year in range(1, 11):
        pri_f = pri_f * (1 + apr_f)
        bar = Rectangle(Point(year, 0), Point(year + 1, pri_f))
        bar.setFill('green')
        bar.setOutline('blue')
        bar.setWidth(2)
        bar.draw(win)

    print('click to quit.')
    win.getMouse()

main()

7.

from graphics import *
import math

def main():
    print('This program calculates and labels the crosspoints between circle and line.')
   
    #draw the windows
    win = GraphWin('CrossPoints', 400, 400)
    win.setBackground('white')
    win.setCoords(-40, -40, 40, 40)

    #inputs
    radius = float(input("the radius:"))
    length = float(input("the length:"))

    #calculate the CrossPoint
    x_1 = math.sqrt(radius ** 2 - length ** 2)
    x_2 = - math.sqrt(radius ** 2 - length ** 2)
    
    #wait for click
    win.getMouse()

    #draw the circle
    cir = Circle(Point(0.0, 0.0), radius)
    cir.setOutline('blue')
    cir.setWidth(2)
    cir.draw(win)

    #draw the line
    p1 = Point(x_1, length)
    p2 = Point(x_2, length)
    line = Line(p1, p2)
    line.setFill('blue')
    line.setWidth(2)
    line.draw(win)

    #draw the points
    p1.draw(win)
    p2.draw(win)
    p1.setFill('red')
    p2.setFill('red')

    #print the x value
    print(x_1, x_2)

    #click for quit
    win.getMouse()

main()

8.

from graphics import *
import math

def main():
    print("This pro plot the line, print its k and l besides.")

    #draw the window
    win = GraphWin()
    win.setBackground('white')
    win.setCoords(0.0, 0.0, 10.0, 10.0)
    
    #click the mouse twice
    p1 = win.getMouse()
    p2 = win.getMouse()

    #calculate k, l and middle point.
    dx = p1.getX() - p2.getX()
    dy = p1.getY() - p2.getY()
    k = dy / dx
    l = math.sqrt(dx ** 2 + dy ** 2)
    m_x = p2.getX() + dx / 2
    m_y = p2.getY() + dy / 2

    #wait for click
    win.getMouse()

    #draw the line and its middle point
    line = Line(p1, p2)
    line.setFill('blue')
    line.setWidth(2)
    line.draw(win)

    middle_point = Point(m_x, m_y)
    middle_point.setFill(color_rgb(0, 255, 255))
    middle_point.draw(win)

    #print
    print(k, l)

    #click to quit
    win.getMouse()

main()

11.

from graphics import *
import math

def main():
    print('This pro plot a house.')

    #draw the win
    win = GraphWin('House', 400, 400)
    win.setBackground('white')

    #click 1 and 2 to draw the rec
    p1 = win.getMouse()
    p2 = win.getMouse()
    rec = Rectangle(p1, p2)
    rec.setOutline('black')
    rec.setWidth(2)
    rec.draw(win)

    #click 3 to draw the door
    p3 = win.getMouse()
    door_width = abs(p1.getX() - p2.getX()) / 5
    door_point1 = Point(p3.getX() - door_width / 2, p1.getY())
    door_point2 = Point(p3.getX() + door_width / 2, p3.getY())
    door = Rectangle(door_point1, door_point2)
    door.setOutline('black')
    door.setWidth(2)
    door.draw(win)

    #click 4 to draw the window
    p4 = win.getMouse()
    window_point1 = Point(p4.getX() - door_width / 2, p4.getY() - door_width / 2)
    window_point2 = Point(p4.getX() + door_width / 2, p4.getY() + door_width / 2)
    window = Rectangle(window_point1, window_point2)
    window.setOutline('black')
    window.setWidth(2)
    window.draw(win)

    #click 5 to draw the roof
    p5 = win.getMouse()
    roof_point1 = Point(p1.getX(), p2.getY())
    roof = Polygon(p5, p2, roof_point1)
    roof.setOutline('black')
    roof.setWidth(2)
    roof.draw(win)

    #click to quit
    win.getMouse()

main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值