题目46
from tkinter import *
canvas = Canvas(width=800, height=600, bg='green')
canvas.pack()
points = (100,200,200,200,200,300,100,300,100,200)
canvas.create_line(points, width=1,fill='pink')
canvas.create_rectangle(400,500,500,600,fill='yellow')
mainloop()
题目47
from tkinter import *
canvas = Canvas(width=800, height=600, bg='green')
canvas.pack()
canvas.create_oval(100,100,200,150,fill='purple')
mainloop()
题目48
nums = []
while True:
try:
elm = int(input("请输入一个数,输入其它非数字字符结束:\n"))
nums.append(elm)
except ValueError:
print("当前已经输入的内容是:",nums)
break
def func1(my_list:list):
"""找到数组中最小和最大的数字,然后分别将它们与最后一个和第一个元素交换
Args:
my_list (list): 传入需要被操作的列表
"""
if not len(my_list):
print("传入的列表中没有数据!!!")
return None
tmp = sorted(my_list)
index1 = my_list.index(tmp[-1])
my_list[0], my_list[index1] = my_list[index1], my_list[0]
index1 = my_list.index(tmp[0])
my_list[-1], my_list[index1] = my_list[index1], my_list[-1]
nums1 = nums[:]
print("nums = ", nums)
func1(nums)
print("nums = ", nums)
def find_min_max(my_list:list):
"""先找到最小的,然后找最大的,把他们放入指定的位置
Args:
my_list (list): 查找的范围
"""
def exchange_elm(my_list, arg1, arg2):
result = my_list.index(arg1)
my_list[arg2], my_list[result] = my_list[result], my_list[arg2]
if not len(my_list):
print("传入的列表中没有数据!!!")
return None
list_min = min(my_list)
list_max = max(my_list)
exchange_elm(my_list, list_max, 0)
exchange_elm(my_list, list_min, -1)
print("nums1 = ", nums1)
find_min_max(nums1)
print("nums1 = ", nums1)
题目49
def listmove(my_list:list, m:int):
"""前面的各数向后移动m个位置,最后的m个数字变成最前面的m个数字
Args:
my_list (list): 待处理的列表
m (int): 移动的位置
"""
try:
if len(my_list) < m:
raise("参数m的值已经超过参数my_list的长度")
my_list = my_list[len(my_list) - m:] + my_list[:len(my_list) - m]
print("my_list = ", my_list)
for i in range(m):
my_list[m + i],my_list[-m+i] = my_list[-m+i],my_list[m+i]
return my_list
except ValueError as ve:
print(ve)
return None
nums = [1,2,3,4,5,6]
print("nums = ", nums)
nums = listmove(nums,2)
print("nums = ", nums)
listmove(nums,10)
题目50
def pop_who_is_three(nums: list, begin: int, out_num: int = 3):
print(f"left list = {nums}, current begin = {begin}")
tmp = []
if len(nums) < 2:
return nums
for x in nums:
if begin % 3:
tmp.append(x)
begin += 1
if begin > 3:
begin = 1
return pop_who_is_three(tmp, begin, out_num)
def pop_who_is_three1(nums: int):
out_num = 3
begin = 1
tmp = nums[:]
while True:
nums1 = tmp[:]
tmp.clear()
if len(nums1) < 2:
return nums1
for x in nums1:
if begin % 3:
tmp.append(x)
begin += 1
if begin > 3:
begin = 1
my_nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("my_nums:", my_nums)
print(pop_who_is_three(my_nums.copy(), 1, 3))
print(pop_who_is_three1(my_nums))