python习题练习(六)

参考书籍:python程序设计

chapter8.

test

from graphics import *

def handlekey(k, win):
    if k == "r":
        win.setBackground("pink")
    if k == "w":
        win.setBackground("white")
    if k == "g":
        win.setBackground("lightgray")
    if k == "b":
        win.setBackground("lightblue")

def handleclick(pt, win):
    #create an Entry for user to type in
    entry = Entry(pt, 10)
    entry.draw(win)

    #Go modal: loop until user types  key
    while True:
        key = win.getKey()
        if key == "Return": break

    #undraw the entry and draw text0
    entry.undraw()
    typed = entry.getText()
    Text(pt, typed).draw(win)

    #clear(ignore) any mouse click that occurered during text entry
    win.checkMouse()

def main():
    win = GraphWin('Click and Type', 500, 500)

    #Event Loop: handle key press and mouse clicks until the user presses the "q" key.
    while True:
        key = win.checkKey()
        if key == "q": #loop exit
            break
        if key:
            handlekey(key, win)
        
        pt = win.checkMouse()
        if pt:
            handleclick(pt, win)

    win.close()

main()

4.

#This pro print the Syracuse sequence.
#input: an int number
#output: seq.
def main():
    n = int(input("Enter an int num:"))
    l = [n]
    while n > 1:
        #process
        if n % 2 == 0: #
            n = n / 2
        else:
            n = 3 * n + 1
        l.append(int(n))
    
    print(l)

main()

5.

#This pro check whether the num is a prime number.
#input: num > 2
#output : prime or not
def main():
    n = int(input("Enter an int num (num > 2):"))
    f = "is"
    for i in range(2, n):
        if n % i == 0:
            f = "is not"
            break

    print("The num {} prime.".format(f))

main()

6.

#This pro list all prime nums le than n.
#input: num
#output: a list.
def isPrime(n):
    #return True if n is a prime number, whether false.
    f = True
    for i in range(2, n):
        if n % i == 0:
            f = False
            break
    return f

def main():
    n = int(input("Enter a number: "))
    l = []
    i = 3
    while i <= n:
        if isPrime(i): l.append(i)
        i += 1
    print(l)

if __name__ == "__main__":
    main()

7.

#This pro finds two prime numbers with their add equal to the given even num.
#input: a even num
#output: two prime nums.
def isPrime(n):
    #return True if n is a prime number, whether false.
    f = True
    for i in range(2, n):
        if n % i == 0:
            f = False
            break
    return f

def isEven(n):
    #return true if n is a even number, whether false.
    f = True
    if n % 2 == 1:
        f = False
    return f

def allPrime(n):
    #return a list, with all prime nums le than n.
    l = []
    i = 3
    while i <= n:
        if isPrime(i): l.append(i)
        i += 1
    return l

def main():
    num = int(input("Enter an even number: "))
    if not isEven(num):
        print("Even number, please!")
        return
    
    #get all prime nums le than n in a list.
    l = allPrime(num)

    for i in l:
        j = num - i
        if isPrime(j):
            ans = [i, j]
            break
    print(ans)
        
if __name__ == "__main__":
    main()

8.

#calculates the gcd of two natural num.
#input: two natural num.
#output: gcd.
def main():
    m, n = input("Enter two natural num: ").split(',')
    m = int(m) 
    n = int(n)
    #n is lager.
    if m > n: m, n = n, m

    while m:
        n, m = m, n % m
    print(n)
    

if __name__ == "__main__":
    main()

13.

#This pro plots the regression line between points.
from graphics import *

def drawWin():
    #draw a window
    win =  GraphWin("regression line", 500, 500)
    win.setBackground("white")
    win.setCoords(0, 0, 100, 100)
    return win

def drawDone(win):
    #draw the done label
    rec = Rectangle(Point(3, 3), Point(13, 8))
    rec.setWidth(2)
    rec.setOutline('black')
    rec.setFill('white')
    rec.draw(win)

def drawText(win):
    tex = Text(Point(8, 5.5), "Done")
    tex.setTextColor('black')
    tex.draw(win)

def drawPoint(p, win):
    #draw the point on the window.
    p.setFill('black')
    p.draw(win)

def isDone(p):
    #return ture if done, otherwise false.
    ##suppose know the boundary of done rectangle.
    p_x = p.getX()
    p_y = p.getY()
    d = False
    if 3 <= p_x <= 13 and 3 <= p_y <= 8:
        d = True
    return d

def SumIssue(p, sum_x, sum_y, sum_xx, sum_xy):
    p_x = p.getX()
    p_y = p.getY()
    sum_x += p_x
    sum_y += p_y
    sum_xx += p_x * p_x
    sum_xy += p_x * p_y
    return sum_x, sum_y, sum_xx, sum_xy

def drawLine(sum_x, sum_y, sum_xx, sum_xy, count, win):
    #calculate m
    ave_x = sum_x / count
    ave_y = sum_y / count
    m = (sum_xy - count * ave_x * ave_y) / (sum_xx - count * ave_x * ave_x)
    #(x0, y0) and (x1, y1)
    x0 = 0
    y0 = ave_y + m * (x0 - ave_x)
    x1 = 100
    y1 = ave_y + m * (x1 - ave_y)
    #draw the line
    line = Line(Point(x0, y0), Point(x1, y1))
    line.setWidth(2)
    line.draw(win)

def main():
    #background
    win = drawWin()
    drawDone(win)
    drawText(win)
    #sum initial
    sum_x, sum_y, sum_xx, sum_xy, count= 0, 0, 0, 0, 0
    #loop
    while True:
        p = win.getMouse()
        if isDone(p): break
        #draw the point
        drawPoint(p, win)
        #sum
        sum_x, sum_y, sum_xx, sum_xy = SumIssue(p, sum_x, sum_y, sum_xx, sum_xy)
        #count
        count += 1
    #draw the redression line
    drawLine(sum_x, sum_y, sum_xx, sum_xy, count, win)
    #click to quit
    win.getMouse()
    win.close()

if __name__ == "__main__":
    main()

14.

#This pro changes the rgb of a ppm pic.
from graphics import *

def drawWin():
    #draw a window
    win =  GraphWin("regression line", 500, 500)
    win.setBackground("white")
    win.setCoords(0, 0, 100, 100)
    return win

def main():
    #draw win
    win = drawWin()
    #open pic
    filename = input("Enter the pic's name: ")
    pic = Image(Point(50, 50), filename)
    pic.draw(win)
    #pic's information
    for i in range(pic.getHeight()):
        for j in range(pic.getWidth()):
            r, g, b = pic.getPixel(j, i)
            brightness = int(round(0.299 * r + 0.587 * g + 0.114 * b))
            pic.setPixel(j, i, color_rgb(brightness, brightness, brightness))
        update(30)
    print("done!")

    newname = input("save as another name: ")
    pic.save(newname)

    #click to quit
    win.getMouse()
    win.close()

if __name__ == "__main__":
    main()

 

你可能感兴趣的:(PYTHON)