How to stop the python turtle from drawing

2024/10/13 2:17:25

Can anyone tell me why this code always has a line on the screen and also how to stop it?

Slight problem with this is that every time this happens, I always get a line on my canvas no matter what I try to do. Any ideas on how to prevent this?

Now this is probably too much code, but I have no idea how else to show the failing code. I have tried a variety of ways but none seem to work.

def drawpixel(x, y, colour):turtle.goto(x, y)turtle.dot(1, colour)def main():screen = turtle.Screen()screen.title('Mandelbrot set drawer')screen.bgcolor('#22ccaa')turtle.hideturtle()turtle.speed(0)turtle.penup()turtle.tracer(-10000, 0)turtle.speed(0)width = int(turtle.numinput('Screen size', 'What width?', 600, 100, 20000))height = int(turtle.numinput('Screen size', 'What height?', 600, 100, 10000))turtle.setworldcoordinates((-width)//2, (-height)//2, (width)//2, (height)//2)radius = 2turtle.goto((-width)//2, (-height)//2)x, y = ((-width)//2, (-height)//2)while y<((height)//2 +1):while x!=((width)//2 +1):newx = x/(width//2)*radiusnewy = y/(width//2)*radiusmpos = newx + newy*1jdrawpixel(x, y, getcolour(mpos))x += 1y += 1turtle.goto((-width)//2, y)

Maybe the getcolour is failing so here is the code for it:

def iterc(c):iters = 0z = 0+0jwhile iters < 16 and math.hypot(z.real, z.imag)<2:z = z*z+citers += 1return itersdef getcolour(pos):x = iterc(pos)colournum = int(x*6.25)colour = 'gray{}'.format(colournum)
Answer

I've reworked your code to get rid of the line and fix other issues that I saw:

def main():screen = turtle.Screen()screen.title('Mandelbrot Set')screen.bgcolor('#22ccaa')width = int(screen.numinput('Screen size', 'What width?', 600, 100, 20000))height = int(screen.numinput('Screen size', 'What height?', 600, 100, 10000))screen.setworldcoordinates(-width // 2, -height // 2, width // 2, height // 2)screen.tracer(0, 0)radius = 2turtle.penup()turtle.hideturtle()turtle.speed('fastest')x, y = -width // 2, -height // 2turtle.goto(x, y)while y < (height // 2):while x < (width // 2):newx = x / (width // 2) * radiusnewy = y / (width // 2) * radiusmpos = newx + newy * 1jdrawpixel(x, y, getcolour(mpos))x += 1x, y = -width // 2, y + 1screen.update()

Despite its title, I don't picture this drawing a Mandelbrot but it should at least scan correctly.

UPDATE

Now that you've provided getcolour(), the Mandelbrot nature of this becomes clear. Below is the complete reworked code and some output:

import math
import turtledef iterc(c):iters = 0z = 0 + 0jwhile iters < 16 and math.hypot(z.real, z.imag) < 2:z = z * z + citers += 1return itersdef getcolour(pos):x = iterc(pos)colournum = int(x * 6.25)return 'gray{}'.format(colournum)def drawpixel(x, y, colour):turtle.goto(x, y)turtle.dot(1, colour)def main():screen = turtle.Screen()screen.title('Mandelbrot Set')screen.bgcolor('#22ccaa')width = int(screen.numinput('Screen size', 'What width?', 600, 100, 20000))height = int(screen.numinput('Screen size', 'What height?', 600, 100, 10000))screen.setworldcoordinates(-width // 2, -height // 2, width // 2, height // 2)screen.tracer(0, 0)radius = 2turtle.penup()turtle.hideturtle()turtle.speed('fastest')x, y = -width // 2, -height // 2turtle.goto(x, y)while y < (height // 2):while x < (width // 2):newx = x / (width // 2) * radiusnewy = y / (width // 2) * radiusmpos = newx + newy * 1jdrawpixel(x, y, getcolour(mpos))x += 1x, y = -width // 2, y + 1screen.update()main()

OUTPUT (reduced in size, still not finished running)

enter image description here

Neither the prettiest nor the fastest implementation but it is a Mandelbrot.

Now that you've seen your code works on my system, you need to review your environment (Python version, Windows or Unix, etc.) to see what's different. The above was done with Python 3.6.0 on Max OSX 10.11

https://en.xdnf.cn/q/118132.html

Related Q&A

Replace values in a string

So the challenge was to replace a specific word in a sentence with asterisks with equivalent length to that word - 3 letters 3 asterisks etc.Section One does not work, but Section Two does - can anyon…

Select n data points from plot

I want to select points by clicking om them in a plot and store the point in an array. I want to stop selecting points after n selections, by for example pressing a key. How can I do this? This is wha…

Python azure uploaded file content type changed to application/octet-stream

I am using python Azure sdk. When the file uploaded its content type changed to application/octet-stream. I want to set its default content type like image/png for PNG image.I am using following method…

the dumps method of itsdangerous throws a TypeError

I am following the guide of 『Flask Web Development』. I want to use itsdangerous to generate a token, but some problems occured. Here is my code:def generate_confirmation_token(self, expiration=3600):…

SP 500 List python script crashes

So I have been following a youtube tutorial on Python finance and since Yahoo has now closed its doors to the financial market, it has caused a few dwelling problems. I run this codeimport bs4 as bs im…

sleekxmpp threaded authentication

so... I have a simple chat client like so:class ChatClient(sleekxmpp.ClientXMPP):def __init__(self, jid, password, server):sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True)self.add_event_han…

DoxyPy - Member (variable) of namespace is not documented

I get the error message warning: Member constant1 (variable) of namespace <file_name> is not documented. for my doxygen (doxypy) documentation. I have documented the file and all functions and cl…

to_csv append mode is not appending to next new line

I have a csv called test.csv that looks like:accuracy threshold trainingLabels abc 0.506 15000 eew 18.12 15000And then a dataframe called summaryDF that looks like:accu…

Optional keys in string formats using % operator?

Is is possible to have optional keys in string formats using % operator? I’m using the logging API with Python 2.7, so I cant use Advanced String Formatting.My problem is as follow:>>> impor…

HDF5 headers missing in installation of netCDF4 module for Python

I have attempted to install the netCDF4 module several times now and I keep getting the same error:Traceback (most recent call last):File "<string>", line 17, in <module>File &quo…