如何将值传递给函数

时间:2011-12-23 11:12:20

标签: python

我是python的新手并做这个功课。我需要用菜单创建一个小程序。我现在非常好。我有点失落。如何将值传递给函数?你能否检查我的代码中是否有其他错误。

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

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

1 个答案:

答案 0 :(得分:4)

有关如何定义和调用函数,请参阅Python tutorial

要进行代码审核,请尝试https://codereview.stackexchange.com/