【问题标题】:why does it bring up local variable 'size' in the call shape function?为什么它会在调用形状函数中显示局部变量“大小”?
【发布时间】:2015-07-28 05:30:36
【问题描述】:

我不知道为什么会出现这个错误:

代码如下`"""shape program,jordan Hampson"""

def main():
"""main boss function"""
shape = ""
while shape != "q":

    shape = "" 
    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"""

    if shape == "rectangle":
        height = int(input("Enter height: "))
        while get_valid_size_rectangle_height(height):
            height = int(input("Enter height: "))
            width = int(input("Enter width: "))        
            while get_valid_size_rectangle_width(width):
                width = int(input("Enter width: "))
                print_rectangle(height, width)

    else:

        size = int(input("Enter size: "))
    while get_valid_size(size): 
        size = int(input("Enter size: "))

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

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

def get_valid_size_rectangle_width(height):
    """checks to see if the rectangle size is valid"""
    if height < 1: 
        print("Value must be at least 1")
        return True 
    else:
        return False         

def get_valid_size_rectangle_height(width):
    """checks to see if the rectangle size is valid"""
    if width < 1: 
        print("Value must be at least 1")        
        return True 
    else:
        return False 

def get_valid_size(size):
    """checks to see if the size is valid"""
    if shape == "triangle" or "square" and size <= 0: 
        print("Value must be at least 1")
        return True 
    else:
        return False 

def get_valid_shape(shape):
    """gets a valid shape"""
    shape_1 = shape
    shapes = ["square", "triangle", "q", "rectangle"]
    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 -_)) * "*")  

def print_rectangle(height, width):
    """prints a rectangle using height and width"""
    rec = (width * "*" for a in range(height))
    print('\n'.join(rec))    

main()

错误信息是

Traceback(最近一次调用最后一次): 文件“C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py”,第 109 行,在 文件“C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py”,第 19 行,在 main 文件“C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py”,第 44 行,在 call_shape builtins.UnboundLocalError:赋值前引用的局部变量“大小”

【问题讨论】:

    标签: python while-loop variable-assignment


    【解决方案1】:

    看看这里的简化代码:

    if shape == "rectangle":
        # size not declared here...
    else:
       size = int(input("Enter size: "))
    
    while get_valid_size(size): 
    

    如果shape 确实是"rectangle",则不会声明size,并且您不能在while 循环中使用它。解决此问题的一种方法是使用无效值预先声明它:

    size = -1
    if shape == "rectangle":
        # size not declared here...
    else:
       size = int(input("Enter size: "))
    
    while get_valid_size(size): 
    

    【讨论】:

    • 我刚试过这个,现在有一个新错误'输入要绘制的形状(q 退出):矩形输入高度:5 回溯(最后一次调用):文件“C:\ Program Files( x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py",第 109 行,在 文件 "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py",行19,在主文件“C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py”中,第 44 行,在 call_shape 文件中“C:\Program Files (x86)\Wing IDE 101 5.1\ src\debug\tserver_sandbox.py",第 72 行,在 get_valid_size builtins.NameError: name 'shape' is not defined'
    【解决方案2】:

    这似乎是一个缩进错误。从while get_valid_size(size): 到该函数末尾的所有内容都应向右移动一个缩进,以便包含在else 块中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 2015-10-04
      • 1970-01-01
      • 2013-06-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多