Python TypeError:' tuple'对象不可调用

时间:2018-06-18 23:59:00

标签: python turtle-graphics

这是我收到的错误:

, line 53, in draw_dots_in_circle
    x, y = get_random_location()
TypeError: 'tuple' object is not callable

我一直在努力工作数小时,需要帮助。我需要它做的就是计数并制作我的十个点。以下是我的代码:

import turtle
import random
from random import randint

from UsefulTurtleFunctions import drawLine
from UsefulTurtleFunctions import writeText
from UsefulTurtleFunctions import drawPoint
from UsefulTurtleFunctions import drawCircle
from UsefulTurtleFunctions import drawRectangle

y = random.randint(-125,100)
x = random.randint(-50, 50)

get_random_location = (x, y)


drawRectangle(x, y, 60, 40)

drawCircle(x, y, 80)

def is_point_in_square(x, y):
    if -125 <= x <= -25 and -50 <= y <= 50:
        return True
    else:
        return False

def draw_dots_in_square(x, y):
    count = 0
    while count < 10:
        x, y = get_random_location()
        if is_point_in_square(x,y):
            drawPoint(x, y)
            count += 1



def is_point_in_circle(x, y):
    d = ((x - 50) ** 2 + y ** 2) ** 0.5
    if d <= 50:
        return True
    else:
        return False
def draw_dots_in_circle(x, y):
    count = 0
    while count < 10:
        x, y = get_random_location()
        if is_point_in_circle(x,y):
            drawPoint(x, y)
            count += 1

def main():
    print (draw_dots_in_circle(x, y))
    print (draw_dots_in_square(x, y))

main()

2 个答案:

答案 0 :(得分:0)

x, y = get_random_location()更改为:

x, y = get_random_location

错误非常明显。 get_random_location是一个元组。你试着打电话。

尽管如此,我认为你打算表达的是:

def get_random_location():

    y = random.randint(-125,100)
    x = random.randint(-50, 50)

    return x, y

然后你可以随意调用它

x, y = get_random_location()

答案 1 :(得分:0)

我同意@raulferreira您可能希望get_random_location成为函数而不是变量。但是,您的代码还存在其他问题,导致其无法正常工作:

  • 如果is_point_in_circle()不知道,is_point_in_square()如何发挥作用 圆的半径或点是什么? (或者,如果它知道 点,那么圆圈在哪里?)

  • 同样import turtle from random import randint from UsefulTurtleFunctions import drawPoint from UsefulTurtleFunctions import drawCircle from UsefulTurtleFunctions import drawRectangle def get_random_location(): x = randint(-50, 50) y = randint(-125, 100) return x, y def is_point_in_rectangle(x, y): rx, ry = rectangle_location return rx - rectangle_width/2 <= x <= rx + rectangle_width/2 and ry - rectangle_height/2 <= y <= ry + rectangle_height/2 def draw_dots_in_rectangle(x, y): count = 0 while count < 10: x, y = get_random_location() if is_point_in_rectangle(x, y): drawPoint(x, y) count += 1 def is_point_in_circle(x, y): cx, cy = circle_location d = ((x - cx) ** 2 + (y - cy) ** 2) ** 0.5 return d <= circle_radius def draw_dots_in_circle(x, y): count = 0 while count < 10: x, y = get_random_location() if is_point_in_circle(x, y): drawPoint(x, y) count += 1 x, y = get_random_location() rectangle_location = (x, y) rectangle_width, rectangle_height = 60, 40 drawRectangle(x, y, rectangle_width, rectangle_height) x, y = get_random_location() circle_location = (x, y) circle_radius = 80 drawCircle(x, y, circle_radius) draw_dots_in_circle(x, y) draw_dots_in_rectangle(x, y) turtle.done() 关于实际身高和身高 宽度。

我对您的代码进行了返工,我最好猜测了UsefulTurtleFunctions.py中的功能:

posts

根据您的作业要求进行调整。

enter image description here