【发布时间】: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