我确信人们会继续问这个问题,但为什么我一直在为我的程序获取TypeError代码?
以下代码名为q1
def rect_bounds(x, y, px, py):
x = 0
y = 0
px = 0
py = 0
if px < x and py < y:
print("point is inside rectangle")
elif px == x or py == y:
print("point is on edge of rectangle")
else:
print("out of bounds")
return x, y, px, py
此代码称为q1_test
import q1
x = int(input("Enter the length in the x-direction: "))
y = int(input("Enter the length in the y-direction "))
px = int(input("Enter a x-coordinate: "))
py = int(input("Enter a y-coordinate: "))
x, y, px, py = q1.rect_bounds(x, y, px, py) #error comes up here
print()
基本上,代码应该向用户询问x和y方向的长度,然后要求用户输入x和y坐标。然后代码告诉用户该点是在矩形中,在矩形的边缘上,还是在矩形的外部。我在标记的地方不断收到错误,但我无法弄明白为什么会这样。
错误:
Traceback (most recent call last): File "C:\Users\Michael\Dropbox\mdocs\CP104 Workspace\ostr1470_a9\src\q1_test.py", line 18, in <module> x, y, px, py = q1.rect_bounds(x, y, px, py) TypeError: 'NoneType' object is not iterable
错误不再出现。相反,它返回: 输入x方向的长度:5 输入y方向的长度5 输入x坐标:2 输入y坐标:2 点位于矩形的边缘
(显然不是这样)
答案 0 :(得分:4)
看起来rect_bounds
没有返回任何内容。无法将q1.rect_bounds(x, y, px, py)
解压缩到四个变量x, y, px, py
中。如果您尝试这样做,您将收到相同的错误:
a, b, c, d = None
此外,在rect_bounds
中,您有:
x = 0
y = 0
px = 0
py = 0
这意味着参数始终设置为零。你应该摆脱这些,以便功能按预期工作。
另外,假设矩形的其他两个边界是x和y轴,则应添加:
if px < x and py < y and 0 < x and 0 < y :
print("point is inside rectangle")
elif px == x or py == y or px == 0 or py == 0:
print("point is on edge of rectangle")
如果矩形的其他边界位于其他位置,则用正确的逻辑替换0.
答案 1 :(得分:2)
您没有从函数返回任何内容,因此结果不可迭代。
>>> a,b,c,d = [1,2,3,4]
>>> a
1
>>> b
2
>>> x,y,z = None # Your function also returns None
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable
答案 2 :(得分:0)
你最初的问题是,当你实际上没有回复时,你告诉来电者你正在归还四件事。你已经解决了这个问题。
让我继续为您解决这个编程问题。你好像很困惑,你来到这里的帮助并不是很重要。
我假设矩形的一角是(0,0)而另一角是(x,y)。
x = int(input("Enter the length in the x-direction: "))
y = int(input("Enter the length in the y-direction "))
px = int(input("Enter a x-coordinate: "))
py = int(input("Enter a y-coordinate: "))
if x>=0 && y>=0:
r = rect_bounds(x,y,px,py);
if r==0:
print('the point is inside')
elif r==1:
print('the point is on the border')
elif r==2:
print('the point is on a corner (both borders)')
elif r==3:
print('the point is outside')
def rect_bounds(x,y,px,py):
if (px>0 && py>0 && px<x && py<y):
c = 0; #inside
elif (px==x||px==0) && py>0 && py<y:
c = 1; #on the border
elif px>0 && px<x && (py==y || py==0):
c = 1; #on border
elif px==x && py==y:
c = 2; #on corner
elif px==x && py==0:
c = 2; #on corner
elif px==0 && py==y:
c = 2; #on corner
elif px==0 && py==0:
c = 2; #on corner
else:
c = 3; #outside
return c
观察我申请的做法: