没有学过python,受邀回答就临时抱佛脚看看API学习了一下,给出作答代码,此代码依赖pythongraphics图形库(http://mcsp.wartburg.edu/zelle/python/graphics.py):
#!/usr/bin/python#-*-coding:utf-8-*-
"""
-------------------------------------------------------------------------------
Function:
MyFirstPythonProgramForTouTiaoQA
Author:Endy
Verison:1.0_2017-03-25
-------------------------------------------------------------------------------
"""
#引用图形库
fromgraphicsimport*;
defdrawCube():
#等待用户输入边长
sideLen=int(input("\n\nPleaseinputsidelengthofthecube."));
#创建绘图窗口
win=GraphWin("CanvasWin",800,800);
#确定前面4个顶点坐标
p1=Point(100,100);
p2=Point(100+sideLen,100);
p3=Point(100+sideLen,100+sideLen);
p4=Point(100,100+sideLen);
#后面相对前面缩小并倾斜,产生透视效果
xOffset=sideLen*0.4;
yOffset=-sideLen*0.3;
sideLen=sideLen*0.8;
p5=Point(100+xOffset,100+yOffset);
p6=Point(100+xOffset+sideLen,100+yOffset);
p7=Point(100+xOffset+sideLen,100+yOffset+sideLen);
p8=Point(100+xOffset,100+yOffset+sideLen);
frontFace=Rectangle(p1,p3);
frontFace.draw(win);
backFace=Rectangle(p5,p7);
backFace.draw(win);
#补充前面到后面的4条边
line=Line(p1,p5);
line.draw(win);
line=Line(p2,p6);
line.draw(win);
line=Line(p3,p7);
line.draw(win);
line=Line(p4,p8);
line.draw(win);
###############################################################################
if__name__=="__main__":
drawCube();