I'm trying to create a row of numbers in python turtle, similar to something like this, with spaces between each number:
0 1 2 3 4 5 6 8 9
This is my code so far in a loop:
import turtle
pen = turtle.Turtle()
numbers = ["0","1","2","3","4","5","6","7","8","9"]
coordinates = 100,100
for i in range(10):
pen.penup()
pen.goto(coordinates)
pen.pendown()
pen.write(numbers[i])
coordinates = coordinates
Whenever I run this, it writes the numbers 0-9, but in the same spot at the coordinates 100,100.
I want each number to be 50 apart, so after the number 0 is at 100,100 the number 1 should be at 100,150. So, at the very end, what would I put after the "coordinates word" so that each number would be 50 apart?
After you write each number, the coordinate needs to be updated as well.
In your last line coordinates = coordinates, you could try
coordinates = (coordinates[0], coordinates[1] + 50)
So that the y value is updated by 50 with each iteration.
Related
I am having trouble solving the following question:
Write a program that draws “modular rectangles” like the ones below. The user specifies the width and height of the rectangle, and the entries start at 0 and increase typewriter fashion from left to right and top to bottom, but are all done mod 10. Example: Below are examples of a 3 x 5 rectangular:
The following code is what I have tried to solve the problem:
I know it's bad but I still don't know how to print the 5 till 9 numbers on top of each other.
width = int(input("Enter the width of the rectangle:"))
height = int(input("Enter the height of the rectangle:"))
for x in range(0, width, 1):
for y in range(0, height, 1):
print(y, end = ' ')
print()
Thank you all in advance.
You're on the right track, but you have a few issues. Firstly you need to iterate y before x, since you process the columns for each row, not rows for each column. Secondly, you need to compute how many values you have output, which you can do with (y*width+x). To output a single digit, take that value modulo 10. Finally range(0, width, 1) is just the same as range(width). Putting it all together:
width = 5
height = 3
for y in range(height):
for x in range(width):
print((y*width+x)%10, end=' ')
print()
Output:
0 1 2 3 4
5 6 7 8 9
0 1 2 3 4
I'm making a Battleship game using Python and Tkinter for GUI but I'm having some difficulties when it comes to the placement of the ships.
I'm using the random library (as rd) but there are 2 problems in my code:
The code only places one squared "ships" instead of varying from 1-5 depending on the length of each ship.
I can't prevent the ships from accumulating (being on top of each other).
Here is the function:
def place_boats(self):
possible_coordinates = [0,1,2,3,4,5,6,7]
possible_directions = ["horizontal","vertical"]
direction = rd.choice(possible_directions)
for i in range(5):
if direction == "horizontal":
row = 0
column = rd.choice(possible_coordinates)
else:
row = rd.choice(possible_coordinates)
size = 1
# self.board_status is an array composed of zeros and ones represent the ships
for j in range(size):
self.board_status[row+i][column] = 1
size += 1
For example, point 1 connects to point 9, then point 2 connects to point 10 and so on for len(pointList).
I am currently stuck because once the turtle attempts to access the 32nd point, it gets an out of bounds error because the point it would need to connect to is point 0 but it wont do so with my current code.
Each point will need to connect to the 9th point ahead of it for the length of the list (40 in this case because of the text file line amount)
I am hinted to use % operator, but I do not know how I would use it.
code
from turtle import *
speed(10)
fileName = input("What is the name of the file? ")
file = open(fileName, 'r', encoding='UTF-8')
pointList = []
penup()
for line in file:
lineTokens = line.split()
x = float(lineTokens[0])
y = float(lineTokens[1])
point = (x,y)
pointList.append(point)
def drawPoints():
for point in pointList:
goto(point)
dot(5, "black")
drawPoints()
i = 0
for point in range(len(pointList)):
print(point)
pencolor("red")
goto(pointList[i])
down()
goto(pointList[i + 9])
up()
i += 1
Text file I am reading from to collect points (if needed)
31.286893008046174 -197.53766811902756
61.80339887498949 -190.21130325903073
90.79809994790936 -178.20130483767358
117.55705045849463 -161.80339887498948
141.4213562373095 -141.4213562373095
161.80339887498948 -117.55705045849463
178.20130483767355 -90.79809994790936
190.21130325903067 -61.803398874989504
197.5376681190275 -31.286893008046214
199.99999999999994 -6.394884621840902e-14
197.5376681190275 31.286893008046086
190.21130325903067 61.80339887498938
178.20130483767352 90.79809994790924
161.80339887498945 117.55705045849447
141.42135623730948 141.42135623730934
117.55705045849464 161.8033988749893
90.7980999479094 178.20130483767338
61.80339887498957 190.2113032590305
31.28689300804629 197.5376681190273
1.5987211554602254e-13 199.99999999999974
-31.286893008045972 197.5376681190273
-61.80339887498924 190.21130325903047
-90.79809994790908 178.20130483767332
-117.55705045849432 161.80339887498926
-141.42135623730914 141.42135623730928
-161.80339887498906 117.55705045849444
-178.20130483767312 90.79809994790921
-190.21130325903022 61.80339887498938
-197.53766811902702 31.286893008046114
-199.99999999999946 -5.329070518200751e-15
-197.53766811902702 -31.286893008046125
-190.2113032590302 -61.803398874989384
-178.20130483767304 -90.7980999479092
-161.80339887498897 -117.5570504584944
-141.421356237309 -141.42135623730923
-117.55705045849416 -161.80339887498914
-90.79809994790895 -178.20130483767318
-61.80339887498913 -190.21130325903027
-31.286893008045876 -197.53766811902707
2.327027459614328e-13 -199.99999999999952
You can use the following code:
goto(pointList[(i + 9)%len(pointList)])
What this does is get a zero if the expression i + 9 evaluates to len(pointList)
Another less elegant solution is:
if i + 9 < len(pointList):
goto(pointList[i + 9])
else:
goto(pointList[0])
I am doing a question that gives me a start coordinate, a end coordinate and the number of times of moving.Every time you can add 1 or minus 1 to x or y coordinate based on previous coordinate and the number of moving limit the time the coordinate can move. At last, I need to identify whether there is a possibility to get to the end coordinate
I decide to use recursion to solve this problem however, it does not end even if I wrote return inside a if else statement. Do you mind to take a look at it.
This is the code
# https://cemc.uwaterloo.ca/contests/computing/2017/stage%201/juniorEF.pdf
# input
start = input()
end = input()
count = int(input())
coo_end = end.split(' ')
x_end = coo_end[0]
y_end = coo_end[1]
end_set = {int(x_end), int(y_end)}
#processing
coo = start.split(' ')
x = int(coo[0])
y = int(coo[1])
change_x = x
change_y = y
sum = x + y+count
set1 = set()
tim = 0
timer = 0
ways = 4** (count-1)
def elit(x, y, tim,timer, ways = ways):
print(tim,timer)
tim = tim +1
co1 = (x, y+1)
co2 = (x+1, y)
co3 = (x, y-1)
co4 = (x-1, y)
if tim == count:
tim =0
set1.add(co1)
set1.add(co2)
set1.add(co3)
set1.add(co4)
print(timer)
timer = timer +1
if timer == ways:
print('hiii')
return co1, co2, co3, co4 #### this is the place there is a problem
elit(co1[0],co1[1],tim,timer)
elit(co2[0],co2[1],tim,timer)
elit(co3[0],co3[1],tim, timer)
elit(co4[0],co4[1],tim, timer)
#print(elit(change_x,change_y,tim)) - none why
elit(change_x,change_y,tim, timer)
#print(list1)
for a in set1:
if end_set != a:
answer = 'N'
continue
else:
answer = "Y"
break
print(answer)
In addition, if you have any suggestions about writing this question, do you mind to tell me since I am not sure I am using the best solution.
one of example is
Sample Input
3 4 (start value)
3 3 (end value)
3 (count)
Output for Sample Input
Y
Explanation
One possibility is to travel from (3, 4) to (4, 4) to (4, 3) to (3, 3).
the detailed question can be seen in this file https://cemc.uwaterloo.ca/contests/computing/2017/stage%201/juniorEF.pdf
It is question 3. Thank you
thank you guys
the function is returning properly however by the time you reach the recursive depth to return anything you have called so many instances of the function that it seems like its in an infinite loop
when you call elite the first time the function calls itself four more times, in the example you have given timer is only incremented every 3 cycles and the function only return once timer hits 16 thus the function will need to run 48 times before returning anything and each time the function will be called 4 more times, this exponential growth means for this example the function will be called 19807040628566084398385987584 times, which depending on your machine may well take until the heat death of the universe
i thought i should add that i think you have somewhat over complicated the question, on a grid to get from one point to another the only options are the minimum distance or that same minimum with a diversion that must always be a multiple of 2 in length, so if t the movement is at least the minimum distance or any multiple of 2 over the result should be 'Y', the minimum distance will just be the difference between the coordinates on each axis this can be found by add in the difference between the x and y coordinates
abs(int(start[0]) - int(end[0])) + abs(int(start[1]) -int(end[1]))
the whole function therefore can just be:
def elit():
start = input('start: ').split(' ')
end = input('end: ').split(' ')
count = int(input('count: '))
distance = abs(int(start[0]) - int(end[0])) + abs(int(start[1]) -int(end[1]))
if (count - distance) % 2 == 0:
print('Y')
else:
print('N')
input:
3 4
3 3
3
output:
Y
input:
10 4
10 2
5
output:
N
So i've got an image in input an i transformed it into an array. There is two ball, and i want to remove one ball.
My idea is to run through a loop, and detect line by line if there is a red pixel. And if in this array at an i, and there is not red pixel in i+1 it erase the entire rest of the line.
for i in range(0, len(data)):
h = h + 1
#print("0"),
if (i>1) and (((data[i - 1])[1] > 40 and (data[i - 1])[2] > 40 ) and ((data[i + 1])[1] > 40 and (data[i+1])[2])):
print("_"),
elif (data[i])[1] < 40 and (data[i])[2] < 40 and (data[i])[0] > 50 :
j = j + 1
print "#" ,
else :
print("."),
#else :
# print data[i],
if h == 64 :
h = 0
test = True
print("\n")
What is wrong with my code and how can i erase a ball through my method ?
If you just want to delete anything on the left side:
data[:,:data.shape[1]/2] = 0
or right side:
data[:,data.shape[1]/2:] = 0
If your problem is that you have balls (red, green and blue) in an array (numpy) and there's no background or noise. For simplicity, if the color of red = 1, green = 2 and blue = 3 then:
data[np.where(data == 1)] = 0
will remove the red ball. For more sophisticated detection-needs you can use below...
You can use label from scipy.ndimage given that your data is an np-array by simply saying (of course if your balls get too overlapped they may not be separated):
from scipy.ndimage import label
labled_data, labels = label(data)
#If you want to remove the first ball
labled_data[np.where(labled_data == 1)] = 0
#Then your second ball will be where the labled data is 2
#Else if you just temporary want to know where the second ball is:
labled_data == 2
#Will be true for those places