How to send turtle to random position?

2024/11/15 19:55:41

I have been trying to use goto() to send turtles to a random position but I get an error when running the program.

I am lost on how else to do this and not sure of other ways. My current code is:

t1.shape('turtle')
t1.penup()
t1.goto((randint(-100,0)),(randint(100,0)))#this is the line with the error

I want the turtle to go to random coordinates in a box between -100,100 and 0,100 but I get the error:

Traceback (most recent call last):File "C:\Users\samdu_000\OneDrive\Documents\python\battle turtles.py",    line 18, in <module>t1.goto((randint(-100,0)),(randint(100,0)))File "C:\Users\samdu_000\AppData\Local\Programs\Python\Python3732\lib\random.py", line 222, in randintreturn self.randrange(a, b+1)File "C:\Users\samdu_000\AppData\Local\Programs\Python\Python37-
32\lib\random.py", line 200, in randrangeraise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, 
istop, width))ValueError: empty range for randrange() (100,1, -99)
Answer

You're asking for a number between 100 and 0. But look at the reference for randint():

random.randint(a, b)

Return a random integer N such that a <= N <= b.

a should be smaller or equal to b. So, replace randint(100,0) with randint(0,100):

import turtle
from random import randintt1 = turtle.Turtle()t1.shape('turtle')
t1.penup()
t1.goto(randint(-100,0),randint(0,100))turtle.done()

Demo: https://repl.it/@glhr/55439167

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

Related Q&A

How to scrape all p-tag and its corresponding h2-tag with selenium?

I want to get title and content of article: example web :https://facts.net/best-survival-movies/ I want to append all p in h2[tcontent-title]and the result expected is: title=[title1, title2, title3]co…

Tkinter: Window not showing image

I am new to GUI programming and recently started working with tKinter.My problem is that the program wont show my image, Im suspecing that it is my code that is wrong, however, I would like somone to e…

print dictionary minus two elements

Python 3.6All debug output is from PyCharm 2017.1.2I have a program that gets to this portion of the code:if len(errdict) == 21:for k, v in errdict.items():if k == packets output or bytes:continueprint…

Write CSV file using Python with the help of a csv dictionary / nested csv dictionary

I am having a csv file and i want to write it to another csv file. Its a bit complicated than it seems. Hoping someone to correct my code and rewrite it, so that i can get the desired csvfile. I am usi…

saving data to txt file using python

I am new in python, and I really need some help. I am doing this memory game where I need to save user, game score and time into a text file using python. I have tried several ways to do it, but nothin…

How can I create bounding boxes/contour around the outer object only - Python OpenCV

So Ive been trying to make bounding boxes around a couple of fruits that I made in paint. Im a total beginner to opencv so I watched a couple tutorials and the code that I typed made, makes contours ar…

resuming download file ftp python3.*

There is a file (1-7Gb) that you need to pick up. The network periodically falls, so it is necessary to implement the method of resume. For example, in 1 communication session downloaded 20% the networ…

printing files based on character

I have a directory(data) that contain thousand of files.Each time I want to select three files that are just differ by only one characterAB[C,D,E] and want to perform some computation on the selected t…

Parsing CSV file using Panda

I have been using matplotlib for quite some time now and it is great however, I want to switch to panda and my first attempt at it didnt go so well.My data set looks like this:sam,123,184,2.6,543 winte…

Getting division by zero error with Python and OpenCV

I am using this code to remove the lines from the following image:I dont know the reason, but it gives me as output ZeroDivisionError: division by zero error on line 34 - x0, x1, y0, y1 = (0, im_wb.sha…