Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 months ago.
Improve this question
I am making a Go game and I've stumbled across a problem with my list.append() method.
Every time a player makes a move I want to append the board (2d list) to my ex_boards list. With every iteration of my for loop, instead of appending the current board to my list, it appends it and changes every past board to the current one in the list, something like ex_boards[every index possible in the list] = current_board. So instead of having every state of the game in my list, I have n (len of the list) times the last updated board.
What don't I understand about classes? Also, my list is right below Class Go.
Try using
ex_boards.append(copy.deepcopy(board))
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have a requirement in which elements in between need to be popped out. Which will be faster for this requirement? A set or a list in python?
When you pop an item from a list, all the filing elements most be shifted, so on average the operation is O(n). Popping the first element is the worst case since the entire list has to shift. Popping the last element is effectively O(1) because nothing needs to shift. As far as I know, python lists don't reallocate to a smaller buffer, so the entire list is only copied when the first element is popped.
Sets are implemented as hashables. Unless you get catastrophic had collision, popping an element is always O(1). If you're popping an arbitrary element, since sets are unordered, it may be O(1) even in the face of collisions, depending on how the buckets are implemented.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For this recursive function when you input a list such as [2,8,3,2,7,2,2,3,2,1,3,0], it will find a path from the first index to the index containing the value 0, but can only move through the list by index[i]+i or index[i]-i and cannot go past the bounds of the list or go to an index that has already been visited. So if you are at index[0] which equals 2, you can only move forward in the list by 2. Or if you are at index[3], you can only move forward or backwards in the list by 2 so you can go to either index[1] or index[5].
For the list [2,8,3,2,7,2,2,3,2,1,3,0] I get a solution which is [0,2,5,7,4,11], but there are more solutions such as [0,2,5,3,1,9,10,7,4,11] and I am not sure how to get my function to continue searching for all the other ones.
Pass in an empty list to start that you keep passing on. Add solutions to it as found.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm recreating Conway's game of life in pygame, and I've been having an issue implementing the preset feature. When I'm assigning the preset array to the default array, and then assigning it back to the default array again, the preset is changed. I'm only editing the array "Initial_frame" but it is somehow changing the preset array.
Code:
https://paste.pythondiscord.com/uvetekikot.py
You are probably doing something like this:
a = [1 2 3 4 5]
b = a
a and b are really arrays, rather they are references to arrays. b=a means they both reference the same array.
You will need to copy the array instead of just copying the reference, like this:
b = a[:]
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
First of all I'm complete beginner. Currently trying to write a simple game using only functions and if-else statements. Basically, I defined some functions and in each function there's a question and corresponding answers. The player will gain points according to the answer. The answer that he gave will lead the player to another function (question) and he/she will also gain points from there. After completing all of these questions, I need to sum all points to say if the player failed or not. I did a search about how to count those points and came up with "global" variables but I failed how to implement them on my program. Besides point count, everything works well. Thanks for the help and sorry for my English.
As you say your a beginner, I would recommend you to take an easier approach than a global variable. Pass the current sum of points to each function with questions. Then in each function add the new points to the sum, if the answse is correct and return the new sum of points.
def question1(points):
"Ask question"
If(user_answer==correct_answer):
points = points + points_gained
return points
Sum = 0
Sum = sum + question1(sum)
Sum = sum + question2(sum)
Continue as showed above, this should work out and is quite simple.
Hope it helps!
Etc.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have started learning Python and was wondering how to make a loop, I cant find a website that has it so if you knew it would be great!
I assume you mean a for loop. You do it like so:
for i in range(10):
print i
This is the simplest way. You can use xrange rather than range for very long loops to help with memory management.
Also there are ways to iterate through iterable objects (like lists, arrays, strings etc.):
a=[1,2,3]
for something in a:
print something
You could also use comprehension to make the code shorter, but that's a bit more advanced and you probably don't need it just yet. Should you so wish to learn what it's about this looks OK for a start.
There are many ways.
while True:
That loops until you put break on a line.
The two Aleksander Lidtke mentioned.
for x in range(10): #Loops through the following code 10 times
And:
x = [1,2,3,4]
for items in x: #the variable items is assigned to
#each variable as it iterates through
To learn more about python, read this book.