I have a problem with loop for game I am writing.
most of game's code is in while loop, the part I am concerned looks more or less like that:
while True:
lista=[]
somenumber=randrange(5,20)
lista.append(somenumber)
break
not a real code but shows idea. I want the list to expand each time the loop runs, but instead the list holds only number from current loop. Any idea how I can do it? I would like the list to expand witch each run of the loop.
You must mive this
lista=[]
Outside of your loop, otherwise you init that variable each iteration. Also you need to delete
break
As it breaks after first iteration. From other hand you need
some logic to stop the loop otherwise you will be spinning till out of memory
Define the list outside of the loop,
Each time it initiates a new list so that your list gets emptied/a new list is defined.
Initialize the list outside while loop.
The list is reinitialized everytime therefore holds only the current iteration value.
Your break statement breaks the loop the first iteration through and your list gets redefined to be empty each iteration. The list should be outside the loop and the loop should only be broken when a certain criteria is met, otherwise it's an infinite loop. This can be accomplished with an if (criteria): break or while (criteria to be met, not yet met):
lista=[]
while True:
somenumber=randrange(5,20)
lista.append(somenumber)
if (some selection statement):
break
Another way to write it, for example if you want 25 items in your list, would be:
lista=[]
while len(lista)<25:
somenumber=randrange(5,20)
lista.append(somenumber)
Try this:
condition = 0 #start
limit = 10 #finish
lista=[]
while condition < limit: #while not yet reached finish
somenumber=randrange(5,20)
lista.append(somenumber)
condition += 1
Thanks guys. I think I know what to do. The game I am doing is an old game from C64, not sure if it was anywhere outside of Poland and in Poland it's title was "Namiestnik" - a text strategy where you were running Roman Colony
Related
I want to have a Python program like--
for i in range(r):
if (i==2):
#change r in some way
which will run the loop for the new range r, after it gets modified in the if statement.
Even if I change r after the if statement,the for loop runs for the initial r I gave.This must be happening because range(r) gets fixed in the for statement in the first line itself,and is not affected by change in r later on.
Is there a "simple way" to bypass this?
By "simple" I mean that I don't want to add a counter which counts how many times the loop already ran and how many times it need to run again after changing(specifically increasing) r,or by replacing for loop with a while loop.
When you say:
for i in range(r):
range(r) creates a range object. It does this only once when the loop is set up initially. Therefore, any changes you make to r inside the loop have no effect on the performance of the loop, since it's the range object that dictates the number of iterations (it just happens to be initialized with r).
Rule of thumb: If you know how many iterations you need in advance, use a for loop. If you don't know how many iterations you need, use a while loop.
I don't believe this is possible. However, using a while loop and a manual counter is itself an extremely simple way to do this.
The code will look something like this:
i = 0
while i < r:
if i == 2:
# Change r in some way
Making a simple program that swaps the location of numbers in a loop until they are in ascending order. I want the program to end when the if conditional is never activated within a instance of the for loop. Is there a shorter way to do this without the use of a while true/false or like?
while tf == True:
for i in range(lisLen-1):
tf=False
if listy[i]>listy[i+1]:
tf=True
swap(listy, i, i+1)
Get rid of the variable, and use break instead. Then you can use the else: clause to test this. That clause runs if the loop ended normally instead of with break.
while True:
for i in range(lisLen-1):
if listy[i]>listy[i+1]:
swap(listy, i, i+1)
break
else:
break
I am wondering if this is possible. I want to have an infinite while loop but still wont the rest of the code outside the while loop to continue running while the loop is on. like a way to break the while loop after each iteration for instance. i need to find a way for the second print statement to be executed without breaking the while loop completely.
while True:
print('i will loop forever')
print('this code will never be executed because of the while loop')
There are a number of ways to accomplish this, such as threading. However, it looks like you may wish to serially loop for a while, break, then continue. Generators excel at this.
for example:
def some_loop():
i=0
while True:
yield i
i+=1
my_loop=some_loop()
#loop for a while
for i in range(20):
print(next(my_loop))
#do other stuff
do_other_stuff()
#loop some more, picking up where we left off
for i in range(10):
print(next(my_loop))
Let’s say I have following simple code:
useText = True
for i in range(20):
if useText:
print("The square is "+ str(i**2))
else:
print(i**2)
I use the variable useText to control which way to print the squares. It doesn’t change while running the loop, so it seems inefficient to me to check it every time the loop runs. Is there any way to check useText only once, before the loop, and then always print out according to that result?
This question occurs to me quite often. In this simple case of course it doesn’t matter but I could imagine this leading to slower performance in more complex cases.
The only difference that useText accomplishes here is the formatting string. So move that out of the loop.
fs = '{}'
if useText:
fs = "The square is {}"
for i in range(20):
print(fs.format(i**2))
(This assumes that useText doesn't change during the loop! In a multithreaded program that might not be true.)
The general structure of your program is to loop through a sequence and print the result in some manner.
In code, this becomes
for i in range(20):
print_square(i)
Before the loop runs, set print_square appropriately depending on the useText variable.
if useText:
print_square = lambda x: print("The square is" + str(x**2))
else:
print_square = lambda x: print(x**2)
for i in range(20):
print_square(i)
This has the advantage of not repeating the loop structure or the check for useText and could easily be extended to support other methods of printing the results inside the loop.
If you are not going to change the value of useText inside the loop, you can move it outside of for:
if useText:
for i in range(20):
print("The square is "+ str(i**2))
else:
for i in range(20):
print(i**2)
We can move if outside of for since you mentioned useText is not changing.
If you write something like this, you're checking the condition, running code, moving to the next iteration, and repeating, checking the condition each time, because you're running the entire body of the for loop, including the if statement, on each iteration:
for i in a_list:
if condition:
code()
If you write something like this, with the if statement inside the for loop, you're checking the condition and running the entire for loop only if the condition is true:
if condition:
for i in a_list:
code()
I think you want the second one, because that one only checks the condition once, at the start. It does that because the if statement isn't inside the loop. Remember that everything inside the loop is run on each iteration.
I saw some similar questions to this but none seems to address this is specific question so I don't know if I am overlooking something since I am new to Python.
Here is the context for the question:
for i in range(10):
if something_happens(i):
break
if(something_happened_on_last_position()):
# do something
From my C background, if I had a for (i=0;i<10;i++) doing the same thing with a break, then the value of i would be 10, not 9 if the break didn't occur, and 9 if it occurred on the last element. That means the method something_happened_on_last_position() could use this fact to distinguish between both events. However what I noticed on python is that i will stop on 9 even after running a successful loop without breaks.
While make a distinction between both could be as simple as adding a variable there like a flag, I never liked such usage on C. So I was curious, is there another alternative to do this or am I missing something silly here?
Do notice that I can't just use range(11) because this would run something_happens(10). It is different on C on this since '10' would fail on the condition on the for loop and would never execute something_happens(10) (since we start from index 0 here the value is 10 on both Python and C).
I used the methods just to illustrate which code chunk I was interest, they are a set of other conditions that are irrelevant for explaining the problem.
Thank you!
It works the other way:
for i in range(10):
if something_happens(i):
break
else: # no break in any position
do whatever
This is precisely what the else clause is for on for loops:
for i in range(10):
if something_happens(i):
break
else:
# Never hit the break
The else clause is confusing to many, think of it as the else that goes with all those if's you executed in the loop. The else clause happens if the break never does. More about this: For/else