未定义python名称以绘制多边形

时间:2016-10-24 13:45:03

标签: python-3.x turtle-graphics

程序说“问题没有定义”的名称为什么? 当我说questio =“大小”时,我定义了它,如果语言=='en'其他\

import locale
def ask_language():
language = input("Language: en, es, ca")
return language
def ask_number(language):
question = "Size: " if language == 'en' else \
           "Tamaño: " if language == 'es' else \
           "Mida: "
size = input(float(question))
locale.setlocale(locale.LC_ALL, "")
if language == 'en':
f_size = float(size)
elif language == 'es':
f_size = locale.atof(size)
else:
f_size = locale.atof(size)
return f_size
def move_turn(turtle, size, degrees):
turtle.forward(size)
turtle.left(degrees)
def polygon(turtle, size, sides):
for _ in range(sides):
    move_turn(turtle, size, 360/sides)
def draw_polygon(size):
import turtle               
window = turtle.Screen()
alex = turtle.Turtle()  
alex.shape( 'turtle' )
alex.speed( 1 )
polygon(alex, size, 5)
window.exitonclick()       
turtle.TurtleScreen._RUNNING = True

1 个答案:

答案 0 :(得分:0)

将代码重新格式化为可运行后,您的直接问题似乎就是这一行:

size = input(float(question))

函数调用已被反转,它应该是:

size = float(input(question))

但鉴于接下来的逻辑,它应该是:

size = input(question)

因为稍后根据语言环境转换答案。下面是代码的返工,以解决上述问题以及一些风格调整:

from turtle import Turtle, Screen
import locale

question_dictionary = {'en': 'Size: ', 'es': 'Tamaño: ', 'ca': 'Mida: '}

def ask_language():
    return input('Language (' + ', '.join(question_dictionary) + '): ')

def ask_number(language):
    size = input(question_dictionary[language])

    locale.setlocale(locale.LC_ALL, '')

    if language == 'en':
        f_size = float(size)
    else:
        f_size = locale.atof(size)

    return f_size

def move_turn(turtle, size, degrees):
    turtle.forward(size)
    turtle.left(degrees)

def polygon(turtle, size, sides):
    for _ in range(sides):
        move_turn(turtle, size, 360 / sides)

def draw_polygon(size):
    alex = Turtle(shape='turtle')
    alex.speed('slowest')  # 'slowest' == 1
    polygon(alex, size, 5)

draw_polygon(ask_number(ask_language()))

window = Screen()

window.exitonclick()