为什么我的代码会继续?

时间:2015-05-15 10:21:20

标签: python for-loop return

此代码应该为用户输入形状类型。然后输入高度然后打印由*组成的形状(现在为三角形/正方形)

代码一直有效,直到用户输入错误输入才能启动,然后输入正确的函数名称并尝试关闭它。任何想法如何解决它?

错误:

Enter shape to draw (q to quit): sad
Unknown shape. Please try again
Enter shape to draw (q to quit): triangle
Enter size: 6
*
**
***
****
*****
******
Enter shape to draw (q to quit): q
Goodbye
Enter size: #this should not be there

整个代码是     """ jordan hampson""""

def main():
    """main boss function"""
    shape = input("Enter shape to draw (q to quit): ").lower()

    if shape == "q":
        print("Goodbye")
        return



    else:
        get_valid_shape(shape)

        call_shape(shape)

        main()



def call_shape(shape):
    """CALLS THE shape"""

    size = int(input("Enter size: "))
    get_valid_size(size)
    if shape == "square":
        return print_square(size)

    elif shape == "triangle":
        return print_triangle(size)

    else: 
        return main()


def get_valid_size(size):
    """checks to see if the size is valid"""
    if size <= 0:
        print("Value must be at least 1")
        main()

def get_valid_shape(shape):
    """gets a valid shape"""
    shape_1 = shape
    shapes = ["square", "triangle", "q"]
    while shape_1 not in shapes:
        print("Unknown shape. Please try again")
        return main()


def print_square(size):
    """prints a square from the input"""
    for _ in range(0, size):
        print(size * "*")


def print_triangle(size): 
    """prints a triangle in *'s"""   
    for _ in range(1, size +1):
        print((size -(size -_)) * "*")  

main()

2 个答案:

答案 0 :(得分:1)

这是由于你的fonction get_valid_shape(),函数调用fonction main()所以当你输入“sad”时你开始一个新的main()但是在fonction get_valid_shape()中(在主进程上你仍然在第get_valid_shape(shape)行。当你按“q”时,你退出这一行并转到call_shape(shape),这个流行的形状是一个三角形,所以它要求你输入一个大小。

为避免这种情况,我建议使用此代码

def main():
    shape = ""
    while shape != "q":
        """main boss function"""
        shape = "" #reinitialize 
        shape = input("Enter shape to draw (q to quit): ").lower()

        if shape == "q":
            print("Goodbye")

        elif get_valid_shape(shape) :
            call_shape(shape)

        else:
            print("Unknown shape. Please try again")



def call_shape(shape):
    """CALLS THE shape"""

    size = int(input("Enter size: "))#ask a 1st time withour print error message
    while get_valid_size(size): #while size is false, ask again
        size = int(input("Enter size: "))

    if shape == "square":
        return print_square(size)

    elif shape == "triangle":
        return print_triangle(size)



def get_valid_size(size):
    """checks to see if the size is valid"""
    if size <= 0: 
        print("Value must be at least 1")
        return True #return true to loop at the while
    else :
        return False #return false to break the while

def get_valid_shape(shape):
    """gets a valid shape"""
    shape_1 = shape
    shapes = ["square", "triangle", "q"]
    if shape_1 not in shapes:
        return False
    else :
        return True


def print_square(size):
    """prints a square from the input"""
    for _ in range(0, size):
        print(size * "*")


def print_triangle(size): 
    """prints a triangle in *'s"""   
    for _ in range(1, size +1):
        print((size -(size -_)) * "*")  

main()

我建议你在python中使用模块pdb。这是一个非常有用的调试模块(你可以看到如何逐步运行你的算法,去某个地方,进入函数等......)link

答案 1 :(得分:0)

您以错误的方式使用了这些return,因为您在代码中没有使用它们,所以您真的不需要任何返回值!

def main():
    """main boss function"""
    shape = input("Enter shape to draw (q to quit): ").lower()

    if shape == "q":
        print("Goodbye")

    else:
        get_valid_shape(shape)
        call_shape(shape)
        main()

def call_shape(shape):
    """CALLS THE shape"""

    size = int(input("Enter size: "))
    get_valid_size(size)
    if shape == "square":
        print_square(size)

    elif shape == "triangle":
        print_triangle(size)

    else: 
        main()

def get_valid_size(size):
    """checks to see if the size is valid"""
    if size <= 0:
        print("Value must be at least 1")
        main()

def get_valid_shape(shape):
    """gets a valid shape"""
    while shape not in ["square", "triangle", "q"]:
        print("Unknown shape. Please try again")
        main()


def print_square(size):
    """prints a square from the input"""
    for _ in range(0, size):
        print(size * "*")


def print_triangle(size): 
    """prints a triangle in *'s"""   
    for _ in range(1, size +1):
        print((size -(size -_)) * "*")  

main()

在我看来,这是一个糟糕的代码,因为你使用了无用的函数,当你可以在没有它们的情况下编写整个代码。

inProgram = True
while inProgram:
    shape = raw_input("Enter shape to draw (q to quit): ").lower()
    while shape not in ["square", "triangle", "q"]:
        shape = raw_input("Unknown shape. Please try again: ").lower()
    if shape == "q":
        print("Goodbye")
        inProgram=False
    else:
        size = raw_input("Enter size: ")
        while not size.isdigit() or size<=0:
            size = raw_input("Value must be at least 1\nTry again: ")
        size = int(size)
        if shape == "square":
            for _ in range(0, size):
                print(size * "*")
        else:
            for _ in range(1, size +1):
                print((size -(size -_)) * "*")  
相关问题