百木园-与人分享,
就是让自己快乐。

Python3.x 基础练习题100例(61-70)

练习61:

题目:
打印出杨辉三角形。  

程序:

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()

输出结果:

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:

题目:
查找字符串。  

程序:

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

输出结果:

2

练习63:

题目:
画椭圆。 
分析:使用 Tkinter。
程序:

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 画图。

程序:

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:

题目:
一个最优美的图案。  

程序:

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.

来源:https://www.cnblogs.com/youcans/p/14470801.html
图文来源于网络,如有侵权请联系删除。

未经允许不得转载:百木园 » Python3.x 基础练习题100例(61-70)

相关推荐

  • 暂无文章