2016-10-24 120 views
-1

程序說名字「問題沒有定義」爲什麼? 我定義它,當我說questio =「大小」,如果語言==「恩」其他\python name not defined to draw polygons

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 

回答

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()