帮助!我明天有这个程序,要求是它必须将topLeft角的X,Y笛卡尔坐标和bottomRight角的X,Y笛卡尔坐标作为raw_input。然后它打印周长,区域和那两个位置。 但是我无法使raw_input工作。我已经尝试将其转换为int,拆分和多个赋值。
topLeft = int(raw_input ('Please enter a coordinate==>')).split()
bottomRight= int(raw_input ('Please enter a coordinate==>')).split()
class Rectangle:
def __init__(self, topLeft, bottomRight):
self.tL = topLeft
self.bR = bottomRight
def perim(self):
return (2 * (self.tL)) + (2 * (self.bR))
def area(self):
return (self.tL) * (self.bR)
def position(self):
return (self.tL, self.bR)
def __str__(self):
return "Rectangle(%s, %s)" % (self.tL, self.bR)
r1 = (Rectangle (topLeft,bottomRight))
print r1
print "Perimeter: %s" % r1.perim()
print "Area: %s" % r1.area()
print "Position: (%s, %s,)" % r1.position()
这是我最接近的尝试,但我仍然收到错误:
Traceback (most recent call last):
File "C:\Users\Mary\Desktop\Python Programs\Rectangle.py", line 1, in <module>
topLeft = int(raw_input ('Please enter a coordinate==>')).split()
ValueError: invalid literal for int() with base 10: '(5,10)'
答案 0 :(得分:0)
这是怎么回事?你犯了很多错误。
tx, ty = raw_input("tx ty: ").split() # the input is "4 5", int("5 4")???
bx, by = raw_input("bx by: ").split()
然后
self.tx = int(tx)
self.ty = int(ty)
# and so on ...
# If you want to work with tuples `(xt,yt)` Make both int first.
。并且所有类方法都不正确,例如perimeter = 2*(bx-tx) + 2*(ty-by)
而不是2*(tx,ty) + 2*(bx,by)
。在语义上它没有意义。