Python类型错误:';元组';对象不可调用

Python类型错误:';元组';对象不可调用,python,turtle-graphics,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

这是我收到的错误:

, 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()
导入海龟
随机输入
从随机导入randint
从UsefulTurtleFunctions导入抽绳
从UsefulTurtleFunctions导入writeText
从UsefulTurtleFunctions导入绘图点
从UsefulTurtleFunctions导入drawCircle
从UsefulTurtleFunctions导入drawRectangle
y=random.randint(-125100)
x=random.randint(-50,50)
获取随机位置=(x,y)
绘图矩形(x,y,60,40)
拉丝圈(x,y,80)
def是平方(x,y)中的点:

如果-125将
x,y=get\u random\u location()
更改为:

x, y = get_random_location
这个错误很明显
get\u random\u location
是一个元组。你在试着叫它

然而,我认为你想要表达的是:

def get_random_location():

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

    return x, y
那你可以随意称呼它

x, y = get_random_location()

我同意@raulferreira的观点,您可能希望
get\u random\u location
成为一个函数而不是一个变量。但您的代码还存在其他问题,无法正常工作:

  • 如果不知道圆中的点()
    如何工作 圆的半径或点是什么?(或者如果它知道 点,那么圆在哪里?)

  • 的同上是相对于实际高度和高度的正方形中的点()
    宽度

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

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()
导入海龟
从随机导入randint
从UsefulTurtleFunctions导入绘图点
从UsefulTurtleFunctions导入drawCircle
从UsefulTurtleFunctions导入drawRectangle
def get_random_location():
x=randint(-50,50)
y=randint(-125100)
返回x,y
def是矩形(x,y)中的点:
rx,ry=矩形_位置

在我的例子中,return rx-rectangle_width/2是错误位置的逗号。我在这个链接中找到了它