1
votes

I have no idea why its bringing up this error:

code as follows `"""shape programme, 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()

the error message is

Traceback (most recent call last): File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py", line 109, in File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py", line 19, in main File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver_sandbox.py", line 44, in call_shape builtins.UnboundLocalError: local variable 'size' referenced before assignment

2

2 Answers

1
votes

Take a look at the simplified code here:

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

while get_valid_size(size): 

If the shape is indeed a "rectangle", size is not declared, and you cannot use it in the while loop. One way around this is to pre-declare it with an invalid value:

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

while get_valid_size(size): 
0
votes

This appears to be an indentation error. Everything from while get_valid_size(size): to the end of that function should be moved one indent to the right, so that it is contained within the else block.