Need Help Replacing Item In List - python

I created a program in python bees,lowers, and trees at different locations and I want the bees to be able to move. However, when I run my program, it always gets stuck because of a type error. Could somebody please explain to me what is causing the issue and how to fix it. Also, I am a complete beginner to programming so any tips on how to make my code faster or more logical would be gladly accepted. The code to the program is below:
import random
wb0,wb1,wb2,wb3,wb4,wb5,wb6,wb7,wb8,wb9 = [],[],[],[],[],[],[],[],[],[]
worker_bees = [wb0,wb1,wb2,wb3,wb4,wb5,wb6,wb7,wb8,wb9]
f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14 = [],[],[],[],[],[],[],[],[],[],[],[],[],[],[]
flowers = [f0,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f14]
t0,t1,t2,t3,t4 = [],[],[],[],[]
trees = [t0,t1,t2,t3,t4]
def world_generate():
def worker_bee_spawn():
x = 0
i = 0
while i < 10:
worker_bees[x] = (random.randrange(0,100), random.randrange(0,100))
x += 1
i += 1
def flower_spawn():
x = 0
i = 0
while i < 15:
flowers[x] = (random.randrange(0,100), random.randrange(0,100))
x += 1
i += 1
if flowers[x - 1] == worker_bees:
x -= 1
i -= 1
def tree_spawn():
x = 0
i = 0
while i < 5:
trees[x] = (random.randrange(0,100), random.randrange(0,100))
x += 1
i += 1
if trees[x - 1] == worker_bees:
x -= 1
i -= 1
elif trees[x - 1] == flowers:
x -= 1
i -= 1
worker_bee_spawn()
flower_spawn()
tree_spawn()
world_generate()
def worker_bee_movement():
x = 0
i = 0
while i < 10:
worker_bee = worker_bees[x]
worker_bee_x = worker_bee[0]
worker_bee_x += 1
worker_bee[0] = worker_bee_x
worker_bees_x = worker_bee
x += 1
i += 1
worker_bee_movement()

Change the following line:
while i < 10:
worker_bee = list(worker_bees[x])
worker_bees[x] is a tuple, so worker_bee = worker_bees[x] then make worker_bee a tuple and your worker_bee[0] = worker_bee_x will fail with:
'tuple' object does not support item assignment
To make it clear, (1,2,3)[0] = 3 is not allowed, [1,2,3][0] = 3 is allowed
And use range to create your lists of lists
worker_bees = [[] for _ in range(10)]
flowers = [[] for _ in range(15)]
trees = [[] for _ in range(4)]

Related

How to arrange print statements in graph, python

I am trying to arrange slashes in print statement to Graph like this
Below is skeleton of my code
y = input("Enter numbers (seperated by space): ")
y_arr = []
y_list = y.split()
for n in range(len(y_list)):
y_arr.append(int(y_list[n]))
space_no = 0
for x in range(len(y_arr)):
value = y_arr[x]
for z in range(value):
if (x%2 == 0):
print (" "*space_no,"\\")
space_no += 1
else:
print (" "*space_no,"/")
space_no += 1
Input statement take numbers and put it into list, then the slashes are generated based on the value of list item. For exa: if value is 2 then 2 slashes will be printed. Even index values go up, odd index values go down. Print statement is printing every character in new line but I want to arrange them like shown in graph.
How to achieve this?
code edited to Python 3
That sounds like homework, but here it is.
y_list = '0 3 1 2 3 6 2 3 6 2 3'.split()
y_arr = []
for n in range(len(y_list)):
y_arr.append(int(y_list[n]))
def cumulative(lists):
cu_list = []
length = len(lists)
cu_list = [sum(lists[0:x:1]) for x in range(0, length+1)]
return cu_list[1:]
space_up = 0
space_dn = 0
last_idx = -1
lines = ['']*max(cumulative([-x if i%2 == 0 else x for i,x in enumerate(y_arr)]))
for x in range(len(y_arr)):
value = y_arr[x]
for z in range(value):
if (x%2 == 0):
lines[last_idx] += (" "*space_dn+"\\")
space_dn = len(lines[last_idx])-len(lines[last_idx-1])
last_idx -= 1
space_up = 0
else:
last_idx += 1
lines[last_idx] += (" "*space_up+"/")
try:
space_up = len(lines[last_idx])-len(lines[last_idx+1])
except IndexError:
pass
space_dn = 0
print('\n'.join(lines[::-1]))

Is there a way to reduce this?

I have this code where st is an array of a class that have x and y parameters. I have to sort their x or y. How can i reduce it so i dont have to write the same code 2 times (one for each case)?
def sort(st, coor):
for i in range(len(st)):
if coor == 'x':
selected = st[i]
j = i - 1
while j >= 0 and st[j].x > selected.x:
st[j + 1], st[j] = st[j], st[j + 1]
j -= 1
st[j + 1] = selected
i += 1
else:
selected = st[i]
j = i - 1
while j >= 0 and st[j].y > selected.y:
st[j + 1], st[j] = st[j], st[j + 1]
j -= 1
st[j + 1] = selected
i += 1
Extract a method for the code that is duplicated.
https://refactoring.guru/extract-method
In that new method, the part that is different is calling the x or y attribute of an instance, and this can be replaced by a call to getattr.

Python 3 error: "IndexError: index 140 is out of bounds for axis 1 with size 100"

from numpy import zeros,linspace
N = 100
points = N**2
x1,x2 = -2,2
y1,y2 = -2,2
m_array = zeros([N,N],float)
i,j = -1,0
for x in linspace(x1,x2,N):
i += 1
for y in linspace(y1,y2,N):
if x == 0 and y == 0:
continue
else:
c = complex(x,y)
z = 0
if abs(c)<2:
for k in range(0,101):
zprime = abs(z) + c
z = zprime
if abs(z) < 2:
m_array[i,j] = 1
j += 1
continue
else:
j += 1
continue
else:
j += 1
continue
N.B. the range of 'k' is arbitrary, it just needs to be relatively large.
I have read through several of the previous questions on the website but I can't seem to find the problem in my own code. This is my attempt at plotting the Mandelbrot set using a density plot.
The continue here applies to the for y in linspace(y1,y2,N): loop - not the for x in linspace(x1,x2,N): loop.
if abs(z) < 2:
m_array[i,j] = 1
j += 1
continue
Therefore, j can be more than 99.

How to use a variable to find another variable in a list - python

(Python 3.x)
z=[]
x=0
while 1==1:
x=x+1
y=1
z.append(x)
while y==1:
a = 0
b = 0
if z(a)==x:
print(x)
y = 2
elif x%z(a)!= 0:
a = a+1
elif b == 2:
y = 2
else:
b = b+1
So, I made a code to find all the prime numbers until python crashes. However, it relies on z(a) for it to work. The idea is that as "a" changes, it moves on in the list.
"z(a)" is where the error lies, so does anyone know a way to fix this?
z is a list. You can approach values inside it by indexes using the z[a] operator (and not z(a) which assumes a function call with a as parameter).
I've taken the liberty of using boolean variables, += operators and unpacking values:
z = []
x = 0
while True:
x += 1
y = True
z.append(x)
while y:
a, b = 0, 0
if z[a] == x:
print(x)
y = False
elif x % z[a]: a += 1
elif b == 2: y = False
else: b += 1
I believe that's what you want to achieve (infinitely incrementing prime generator):
def prime (n): return not any([n % i == 0 for i in range(2, n)])
x = 0
while True:
if prime(x): print(x)
x += 1

Python: syntax assignment error in iteration

I am doing a simple script for self learning in python where the scripts in turn finds the 1000th prime number but I get a syntax error.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:(counter = counter + 1 and integer = integer + 1)
print (y)
when it comes to the ='s assignment right after the ELSE operator and I don't understand why it won't let me add one to both the counter and integer when this has worked in other iteration scenario
In python you can't make an assignment inside an expresion, to avoid misspellings between = and ==. So you must do that in two lines:
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)
try this
else:
counter = counter + 1
integer = integer + 1
In python, assignment to variable has no Boolean value. and mean Boolean operator not do this and this.
so you need to split the statements.
x = 0
y = 2
counter = x
integer = y
while (counter>999):
if (y%2 == 0 or y%3 == 0):
y = y + 1
else:
counter += 1
integer += 1
print (y)

Categories

Resources