使用矩形区域的用户输入,找到所有可能的长度和宽度组合(在 Python 3 中)
Using a user input for the area of a rectangle, find all possible length and width combinations (in Python 3)
我必须要求用户输入 x 和 y 坐标的数据
图形上的点和矩形的面积。然后我必须计算并打印可以从这些数据构建的矩形的所有不同可能性。然后我必须显示原点的 x 和 y 坐标以及对角的 x 和 y 坐标。我可以完成其中的大部分工作,但是从区域(必须是整数)中分解矩形的长度和宽度让我摸不着头脑。必须用循环或语句来完成,除了 main 之外没有新的或单独的函数。
这就是我所做的,但后来我 运行 出错了,列表是空的...它 运行 符合预期,但到那时为止:
`for j in range(0, len(factors)):
l = factors[0]
w = factors[-1]
# Calculate the x- and y-coordinate of the 1st rectangle using the width and the length
first = (x + w,y + l)
print ("The First Rectangle is the one which is constructed between the point", original_coordinate, "and", first)
print ("")
# Step 7. Calculate the x- and y-coordinate of the 2nd rectangle using the width and the length
second = (x - w,y + l)
在 for 循环的末尾,在第八个可能的矩形之后,它会移动到下一个因子对并丢弃第一组,依此类推:
factors.pop(-1)
factors.pop(0)
area = 12 # comes from input
possibilities = [] # initialize a list
for length in range (1, area+1): # go through every possible length
width, invalid = divmod(area, length) # find the width and its validity
if not invalid: # if there's no remainder
possibilities.append((length, width)) # add this
或者作为理解:
possibilities = [(length, width) for length, (width, invalid) in
((length, divmod(area, length)) for length in
range(1, area+1)) if not invalid]
结果:
>>> print(*possibilities, sep='\n') # print the possible rectangles
(1, 12)
(2, 6)
(3, 4)
(4, 3)
(6, 2)
(12, 1)
解决了!
while len(factors)>0:
l = factors[0]
w = factors[-1]
print("The eight possibilites for the lenghth of", l, "and the width of", w, "are: ")
print()
first = (x + w,y + l)
print ("The First Rectangle is the one which is constructed between the point", original_coordinate, "and", first)
print ("")
等Tigerhawk,刚刚看了你最后的评论,这让我想到了 while 而不是 for,谢谢!
我必须要求用户输入 x 和 y 坐标的数据 图形上的点和矩形的面积。然后我必须计算并打印可以从这些数据构建的矩形的所有不同可能性。然后我必须显示原点的 x 和 y 坐标以及对角的 x 和 y 坐标。我可以完成其中的大部分工作,但是从区域(必须是整数)中分解矩形的长度和宽度让我摸不着头脑。必须用循环或语句来完成,除了 main 之外没有新的或单独的函数。
这就是我所做的,但后来我 运行 出错了,列表是空的...它 运行 符合预期,但到那时为止:
`for j in range(0, len(factors)):
l = factors[0]
w = factors[-1]
# Calculate the x- and y-coordinate of the 1st rectangle using the width and the length
first = (x + w,y + l)
print ("The First Rectangle is the one which is constructed between the point", original_coordinate, "and", first)
print ("")
# Step 7. Calculate the x- and y-coordinate of the 2nd rectangle using the width and the length
second = (x - w,y + l)
在 for 循环的末尾,在第八个可能的矩形之后,它会移动到下一个因子对并丢弃第一组,依此类推:
factors.pop(-1)
factors.pop(0)
area = 12 # comes from input
possibilities = [] # initialize a list
for length in range (1, area+1): # go through every possible length
width, invalid = divmod(area, length) # find the width and its validity
if not invalid: # if there's no remainder
possibilities.append((length, width)) # add this
或者作为理解:
possibilities = [(length, width) for length, (width, invalid) in
((length, divmod(area, length)) for length in
range(1, area+1)) if not invalid]
结果:
>>> print(*possibilities, sep='\n') # print the possible rectangles
(1, 12)
(2, 6)
(3, 4)
(4, 3)
(6, 2)
(12, 1)
解决了!
while len(factors)>0:
l = factors[0]
w = factors[-1]
print("The eight possibilites for the lenghth of", l, "and the width of", w, "are: ")
print()
first = (x + w,y + l)
print ("The First Rectangle is the one which is constructed between the point", original_coordinate, "and", first)
print ("")
等Tigerhawk,刚刚看了你最后的评论,这让我想到了 while 而不是 for,谢谢!