Python Turtle程序中sys.argv命令行参数无法正常使用的问题
问题解决与代码修复
错误原因
你遇到的NameError: name 'small' is not defined是因为在Python交互环境直接输入了small,Python会把它当作变量名,但这个变量从未定义。正确的运行方式是在系统命令行中运行脚本并传入参数,比如:
python your_script_name.py small
同时你的代码存在几个需要修复的问题,下面是完整的修正版本:
修正后的代码
from turtle import * from random import randint # 修复多行字符串格式,使用三重引号 HELP_MESSAGE = """Please set the size on the command line Valid options are: small, medium, and large Example: python script.py small""" ERROR_MESSAGE = "Invalid size option! Valid choices: small, medium, large" # 所有函数定义移到主逻辑之前,确保调用时已定义 def stars(side_length): '''Draws and colors the shape of the stars in the sky.''' color('yellow') begin_fill() for i in range(5): forward(side_length) right(144) end_fill() def landscape(length, height): '''Draws a simple rectangle at bottom of the screen for the landscape.''' for i in range(2): forward(length) right(90) forward(height) right(90) def sky_stars(side_length, x_min, x_max, y_min, y_max): '''Randomly inserts the previously defined stars in the night sky.''' for i in range(40): x = randint(x_min, x_max) y = randint(y_min, y_max) penup() goto(x, y) pendown() stars(side_length) ht() def draw_landscape(length, height, x1, y1): '''Draws the rectangle at the correct positioning (bottom of the screen).''' penup() goto(x1, y1) pendown() color('dark green') begin_fill() landscape(length, height) end_fill() def the_moon(radius, x1, y1, x2, y2): '''Draws 2 circles to create the half-moon effect.''' penup() goto(x1, y1) color('white') begin_fill() circle(radius) end_fill() penup() goto(x2, y2) pendown() color('black') begin_fill() circle(radius) end_fill() def mountain_1(side_length, x1, y1): '''Draws the mountain on the left side of the screen.''' penup() goto(x1, y1) pendown() color('grey') begin_fill() for i in range(3): forward(side_length) left(120) end_fill() def mountain_2(side_length, x1, y1): '''Draws the mountain on the right side of the screen.''' penup() goto(x1, y1) pendown() color('grey') # 添加颜色设置,避免默认颜色问题 begin_fill() for i in range(3): forward(side_length) left(120) end_fill() def mountain_3(side_length, x1, y1): '''Draws the larger mountain in the middle of the screen.''' penup() goto(x1, y1) pendown() color('dark grey') begin_fill() for i in range(3): forward(side_length) left(120) end_fill() if __name__ == "__main__": import sys if len(sys.argv) < 2: print(HELP_MESSAGE) sys.exit(1) size = sys.argv[1] # 初始化绘图设置,移到主逻辑内 speed(0) bgcolor('Black') if size == "small": setup(400, 250) sky_stars(5, -200, 200, 0, 125) draw_landscape(400, 250, -200, -50) the_moon(25, -150, 50, -140, 50) mountain_1(150, -200, -50) mountain_2(150, 50, -50) mountain_3(200, -80, -50) elif size == "medium": setup(800, 500) sky_stars(10, -400, 400, 0, 250) draw_landscape(800, 500, -400, -100) the_moon(50, -300, 100, -280, 100) mountain_1(300, -400, -100) mountain_2(300, 100, -100) mountain_3(400, -160, -100) elif size == "large": setup(1600, 1000) sky_stars(20, -800, 800, 0, 500) draw_landscape(1600, 1000, -800, -200) the_moon(100, -600, 200, -560, 200) mountain_1(600, -800, -200) mountain_2(600, 200, -200) mountain_3(800, -320, -200) else: print(ERROR_MESSAGE) sys.exit(1) done() # 添加done()防止绘图窗口立即关闭
关键修改说明
- 函数位置调整:将所有绘图函数移到主逻辑(
if __name__ == "__main__")之前,确保调用函数时已经定义,避免NameError。 - 字符串格式修复:用三重引号包裹多行的HELP_MESSAGE,避免语法错误;新增ERROR_MESSAGE变量,修复else分支的未定义错误。
- 绘图逻辑修正:sky_stars函数中先移动到随机位置再绘制星星,否则所有星星会叠加在初始位置。
- 颜色补全:给mountain_2添加颜色设置,解决山体颜色缺失问题。
- 初始化优化:将speed和bgcolor移到主逻辑内,确保绘图前完成初始化;添加
done()防止绘图窗口绘制完成后立即关闭。
内容的提问来源于stack exchange,提问作者user20352630
相关产品推荐
相关产品推荐

