2011-12-23 7 views
0

저는 파이썬을 처음 접하고이 숙제를하고 있습니다. 메뉴가있는 작은 프로그램을 만들어야합니다. 나는 지금 아주 좋았어. 나는 조금 잃었다. 함수에 값을 전달하려면 어떻게해야합니까? 내 코드에 다른 결함이 있는지 확인해 주시겠습니까?함수에 값을 전달하는 방법

import turtle as t 


def how_many(): 
    while True: 
     print " How many of then do you want to draw?" 
     print " (Range is from 1 to 5)" 
     shape_no = raw_input(' Enter your choice: ') 
     try: 
      shape_no = int(shape_no) 
      if (1 <= shape_no <= 5): 
       print "Your number is ok" 
       break 
      else: 
       print 
       print "from 1 to 5 only" 

     except: 
      print "Only numbers allowed - Please try again" 
    return True 


#=========#=========#=========#=========#=========#=========#=========# 


def draw_square(): 
    t.penup() 
    t.setpos(-200,0) 
    t.pendown() 

    Number_of = 5 
    for a in range(Number_of): 
     for a in range(4): 
      t.forward(60) 
      t.left(90) 
     t.penup() 
     t.forward(80) 
     t.pendown() 

#=========#=========#=========#=========#=========#=========#=========# 
def main(): 
    while True: 
     print 
     print " Draw a Shape" 
     print " ============" 
     print 
     print " 1 - Draw a square" 
     print 
     print " X - Exit" 
     print 

     choice = raw_input(' Enter your choice: ') 

     if (choice == 'x') or (choice == 'X'): 
      break 
     elif choice == '1': 
      how_many() 
      draw_square() 
     else: 
      print 'Try again' 



#=========#=========#=========#=========#=========#=========#=========# 
if __name__ == "__main__": 
    main() 

#=========#=========#=========#=========#=========#=========#=========# 
+0

어떤 기능을합니까? – sheepez

+0

사용자가 얼마나 많은 사각형을 그려야 하는지를 그릴 수 있기를 원합니다. – emre

답변

4

기능을 정의하고 호출하는 방법은 Python tutorial을 참조하십시오.

코드 검토를 위해 https://codereview.stackexchange.com/

+0

+1 : 완벽한 답변. –

+0

내가 얻을 나가서 설명하자면 NameError : 전역 이름 'shape_no이'내가'draw_square (shape_no)''데프 draw_square (n)을 수행 할 때 정의되지 않은 : t.penup() t.setpos (-200,0) t.pendown () 범위 NUMBER_OF A에 대한 N = (NUMBER_OF) : (4)의 범위에 대한 : t.forward (60) t.left (90) t.penup() t.forward (80) t.pendown()' – emre

+0

함수에서 값을 반환 할 수 있습니다. [반환] (http://docs.python.org/reference/simple_stmts.html#return) – gecco

관련 문제