You have two infinite loops, which can only be ended by a break
statement. You do have a break
statement in your code, but only one. A break
statement will only break out of the most inner loop that it is in, in this case, your inner while
loop. The outer while
loop therefore will never end. Although there are better ways of writing your code [like Prune has pointed out], I will give a suggestion that follows closely to your original code:
V1 = True
while V1 is True:
print["Checked for vulnerability"]
if my_file.is_file[]:
print['Virus is still there']
time.sleep[6]
os.system["rm /home/levi/Desktop/Trojan"]
else:
print['Virus removed']
V1 = False
print['Did this work']
I
still removed the outer while
-loop, since it serves no purpose in any way. Unless, of course, you were going to add more to the outer loop than just an other loop, then you should re-add it. In that case however, you should define your code in the following way:
loop = True
V1 = True
while loop is True:
while V1 is True:
print["Checked for vulnerability"]
if my_file.is_file[]:
print['Virus is still there']
time.sleep[6]
os.system["rm /home/levi/Desktop/Trojan"]
else:
print['Virus removed']
V1 = False
# do something else here after ending the inner while loop
print['Did this work']
This ensures that the inner while
loop is only ever executed once, no matter how many times the outer loop iterates.
For loops
There are two ways to create loops in Python: with the for-loop and the while-loop.
When do I use for loops
for loops are used when you have a block of code which you want to repeat a fixed number of times. The for-loop is always used in combination with an iterable object, like a list or a range. The Python for statement iterates over the members of a sequence in order, executing the block each time. Contrast the for statement with the ''while'' loop, used when a condition needs to be checked each iteration or to repeat a block of code forever. For example:
For loop from 0 to 2, therefore running 3 times.
for x in range[0, 3]: print["We're on time %d" % [x]]
While loop from 1 to infinity, therefore running forever.
x = 1 while True: print["To infinity and beyond! We're getting close, on %d now!" % [x]] x += 1
When running the above example, you can stop the program by pressing ctrl+c at the same time. As you can see, these loop constructs serve different purposes. The for loop runs for a fixed amount of times, while the while loop runs until the loop condition changes. In this example, the condition is the boolean True which will never change, so it will run forever.
How do they work?
If you've done any programming before, you have undoubtedly come across a for loop or an equivalent to it. Many languages have conditions in the syntax of their for loop, such as a relational expression to determine if the loop is done, and an increment expression to determine the next loop value. In Python, this is controlled instead by generating the appropriate sequence. Basically, any object with an iterable method can be used in a for loop. Even strings, despite not having an iterable method - but we'll not get on to that here. Having an iterable method basically means that the data can be presented in list form, where there are multiple values in an orderly fashion. You can define your own iterables by creating an object with next[] and iter[] methods. This means that you'll rarely be dealing with raw numbers when it comes to for loops in Python - great for just about anyone!
Nested loops
When you have a block of code you want to run x number of times, then a block of code within that code which you want to run y number of times, you use what is known as a "nested loop". In Python, these are heavily used whenever someone has a list of lists - an iterable object within an iterable object.
for x in range[1, 11]: for y in range[1, 11]: print['%d * %d = %d' % [x, y, x*y]]
- Early exits
Like the while loop, the for loop can be made to exit before the given object is finished. This is done using the break statement, which will immediately drop out of the loop and continue execution at the first statement after the block. You can also have an optional else clause, which will run should the for loop exit cleanly - that is, without breaking.
for x in range[3]: if x == 1: break
Examples
For..Else
for x in range[3]: print[x] else: print['Final x = %d' % [x]]
Strings as an iterable
string = "Hello World" for x in string: print[x]
Lists as an iterable
collection = ['hey', 5, 'd'] for x in collection: print[x]
Loop over Lists of lists
list_of_lists = [ [1, 2, 3], [4, 5, 6], [7, 8, 9]] for list in list_of_lists: for x in list: print[x]
Creating your own iterable
class Iterable[object]: def __init__[self,values]: self.values = values self.location = 0 def __iter__[self]: return self def next[self]: if self.location == len[self.values]: raise StopIteration value = self.values[self.location] self.location += 1 return value
Your own range generator using yield
def my_range[start, end, step]: while start