Python3.x 基礎練習題100例(61-70)

練習61:

題目:
打印出楊輝三角形。  canvas

程序:數組

if __name__ == '__main__':
    a = []
    for i in range(10):
        a.append([])
        for j in range(10):
            a[i].append(0)
    for i in range(10):
        a[i][0] = 1
        a[i][i] = 1
    for i in range(2, 10):
        for j in range(1, i):
            a[i][j] = a[i - 1][j - 1] + a[i - 1][j]
    for i in range(10):
        for j in range(i + 1):
            print(str(a[i][j]), end=" ")
        print()

輸出結果:app

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

練習62:

題目:
查找字符串。  函數

程序:oop

sStr1 = 'abcdefg'
sStr2 = 'cde'
print(sStr1.find(sStr2))

輸出結果:code

2

練習63:

題目:
畫橢圓。 
分析:使用 Tkinter。
程序:ip

if __name__ == '__main__':
    from Tkinter import *
    x = 360
    y = 160
    top = y - 30
    bottom = y - 30

    canvas = Canvas(width = 400,height = 600,bg = 'white')
    for i in range(20):
        canvas.create_oval(250 - top,250 - bottom,250 + top,250 + bottom)
        top -= 5
        bottom += 5
    canvas.pack()
    mainloop()

練習64:

題目:
利用ellipse 和 rectangle 畫圖。字符串

程序:input

if __name__ == '__main__':
    from Tkinter import *
    canvas = Canvas(width = 400,height = 600,bg = 'white')
    left = 20
    right = 50
    top = 50
    num = 15
    for i in range(num):
        canvas.create_oval(250 - right,250 - left,250 + right,250 + left)
        canvas.create_oval(250 - 20,250 - top,250 + 20,250 + top)
        canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2))
        right += 5
        left += 5
        top += 10

    canvas.pack()
    mainloop()

練習65:

題目:
一個最優美的圖案。  string

程序:

import math
class PTS:
    def __init__(self):
        self.x = 0
        self.y = 0
points = []

def LineToDemo():
    from Tkinter import *
    screenx = 400
    screeny = 400
    canvas = Canvas(width = screenx,height = screeny,bg = 'white')

    AspectRatio = 0.85
    MAXPTS = 15
    h = screeny
    w = screenx
    xcenter = w / 2
    ycenter = h / 2
    radius = (h - 30) / (AspectRatio * 2) - 20
    step = 360 / MAXPTS
    angle = 0.0
    for i in range(MAXPTS):
        rads = angle * math.pi / 180.0
        p = PTS()
        p.x = xcenter + int(math.cos(rads) * radius)
        p.y = ycenter - int(math.sin(rads) * radius * AspectRatio)
        angle += step
        points.append(p)
    canvas.create_oval(xcenter - radius,ycenter - radius,
                       xcenter + radius,ycenter + radius)
    for i in range(MAXPTS):
        for j in range(i,MAXPTS):
            canvas.create_line(points[i].x,points[i].y,points[j].x,points[j].y)
       
    canvas.pack()
    mainloop()

if __name__ == '__main__':
    LineToDemo()

練習66:

題目:
輸入3個數a,b,c,按大小順序輸出。   

程序:

if __name__ == '__main__':
    n1 = int(input('n1 = :'))
    n2 = int(input('n2 = :'))
    n3 = int(input('n3 = :'))

    def swap(p1, p2):
        return p2, p1
       
    if n1 > n2: n1, n2 = swap(n1, n2)
    if n1 > n3: n1, n3 = swap(n1, n3)
    if n2 > n3: n2, n3 = swap(n2, n3)
       
    print(n1, n2, n3)

輸出結果:

n1 = :5
n2 = :6
n3 = :2
2 5 6

練習67:

題目:
輸入數組,最大的與第一個元素交換,最小的與最後一個元素交換,輸出數組。

程序:

def inp(numbers):
    for i in range(6):
        numbers.append(int(input('輸入一個數字:')))

p = 0

def arr_max(array):
    max = 0
    for i in range(1, len(array) - 1):
        p = i
        if array[p] > array[max]: max = p
    k = max
    array[0], array[k] = array[k], array[0]

def arr_min(array):
    min = 0
    for i in range(1, len(array) - 1):
        p = i
        if array[p] < array[min]: min = p
    l = min
    array[5], array[l] = array[l], array[5]

def outp(numbers):
    for i in range(len(numbers)):
        print(numbers[i])

if __name__ == '__main__':
    array = []
    inp(array)  # 輸入 6 個數字並放入數組
    arr_max(array)  # 獲取最大元素並與第一個元素交換
    arr_min(array)  # 獲取最小元素並與最後一個元素交換
    print('計算結果:')
    outp(array)

輸出結果:

輸入一個數字:1
輸入一個數字:2
輸入一個數字:5
輸入一個數字:8
輸入一個數字:7
輸入一個數字:6
計算結果:
8
2
5
6
7
1

練習68:

題目:
有n個整數,使其前面各數順序向後移m個位置,最後m個數變成最前面的m個數

程序:

if __name__ == '__main__':
    n = int(input('整數 n 爲:\n'))
    m = int(input('向後移 m 個位置爲:\n'))

    def move(array, n, m):
        array_end = array[n - 1]
        for i in range(n - 1, -1, - 1):
            array[i] = array[i - 1]
        array[0] = array_end
        m -= 1
        if m > 0: move(array, n, m)
       
    number = []
    for i in range(n):
        number.append(int(input('輸入一個數字:\n')))
    print('原始列表:', number)
       
    move(number, n, m)
       
    print('移動以後:', number)

輸出結果:

整數 n 爲:
5
向後移 m 個位置爲:
2
輸入一個數字:
1
輸入一個數字:
2
輸入一個數字:
3
輸入一個數字:
4
輸入一個數字:
5
原始列表: [1, 2, 3, 4, 5]
移動以後: [4, 5, 1, 2, 3]

練習69:

題目:
有n我的圍成一圈,順序排號。從第一我的開始報數(從1到3報數),凡報到3的人退出圈子,問最後留下的是原來第幾號的那位。

程序:

if __name__ == '__main__':
    nmax = 50
    n = int(input('請輸入總人數:'))
    num = []
    for i in range(n):
        num.append(i + 1)

    i = 0
    k = 0
    m = 0
       
    while m < n - 1:
        if num[i] != 0: k += 1
        if k == 3:
            num[i] = 0
            k = 0
            m += 1
        i += 1
        if i == n: i = 0
       
    i = 0
    while num[i] == 0: i += 1
    print(num[i])

輸出結果:

請輸入總人數:32
4

練習70:

題目:
寫一個函數,求一個字符串的長度,在main函數中輸入字符串,並輸出其長度。

程序:

if __name__ == '__main__':
    s = input('please input a string:\n')
    print('the string has %d characters.' % len(s))

輸出結果:

please input a string:
1215sd4f5
the string has 9 characters.
相關文章
相關標籤/搜索