python3 turtle 画国际象棋棋盘 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan import turtle n = 60 # 每行间隔 x = -300 # x初始值 y = -300 # x初始值 turtle.speed(11) turtle.pensize(2) # 先画8*8的正方形,并按要求涂黑 for i in range(8): for j in range(1, 9): turtle
题目一(输出国际象棋棋盘) 分析: 用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格. 主要代码: for i in range(8): for j in range(8): if (i+j)%2!=0: print(chr(219)*2,end='') else: print(' ',end='') print('') 题目二(排列组合问题) 有1.2.3.4个数字,能组成多少个互不相同且无重复数字的四位数?都是多少? 分析: 我们可以先预测一下,共有2
import turtle step = 40 for i in range(8): for j in range(8): turtle.penup() turtle.goto(i*step, j*step) turtle.pendown() turtle.begin_fill() if (i+j)%2 == 0: turtle.color("white") else: turtle.color("black") for k in range(4): turtle.
import turtle n = 60 # 每行间隔,小格子边长 x = -300 # x初始值 y = -300 # x初始值 def main(): turtle.speed(11) turtle.pensize(2) turtle.penup() # 先画8*8的正方形,并按要求涂黑 for i in range(8): for j in range(8): turtle.goto(x + i * n, y + j * n) if (i + j) % 2 == 0: # 白格子 draw
题目链接:点击打开链接 Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-