Вопрос

Draw a graph with Turtle

import turtle

import random

This is where the code begins

def graph(numX, xWidth, scaleY, radius=2):
    minY = 0
    offsetX = -200
    for xVal in range(numX):

        x = offsetX + (xVal*xWidth)
        rand = random.random()
        y = minY + int(rand*scaleY)
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        turle.circle(radius)

    graph(20, 20, 100, 5)

    turtle.mainloop()

Should it end like this?

Это было полезно?

Решение

It looks like you have an indentation problem, you are calling the graph function within itself. Try the following:

def graph(numX, xWidth, scaleY, radius=2):
    minY = 0
    offsetX = -200
    for xVal in range(numX):

        x = offsetX + (xVal*xWidth)
        rand = random.random()
        y = minY + int(rand*scaleY)
        turtle.penup()
        turtle.goto(x,y)
        turtle.pendown()
        turle.circle(radius)

graph(20, 20, 100, 5)

turtle.mainloop()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top