This is a simple code to find and print the position of "zip" of the last occurrence after the first one, so if no "zip" was found or it only occurred once it should print -1. I wrote two code which can do the same thing and I want to know which is a better code and why and how to improve it (besides adding comments).
At first I wrote this code
text = 'all zip files are zipped'
indicator = 0
zip_position = text.find("zip" , indicator)
printer = zip_position
cycles = 0
printer_confirmation = 0
while zip_position != -1:
printer = zip_position
zip_position = text.find("zip" , indicator)
indicator = zip_position + 1
if printer > zip_position and cycles > 1:
print printer
printer_confirmation = 1
cycles += 1
if cycles < 3 and printer_confirmation != 1:
print -1
Then I wrote the second code after a hint which was this line: " print (text.find('zip', text.find("zip")+1)) "
my full code:
text = "all zip files are zipped"
indicator = 1
zip_position = (text.find('zip', text.find("zip")+indicator))
while zip_position != -1:
zip_position = (text.find('zip', text.find("zip")+indicator))
indicator += 1
if zip_position == -1 and text.find("zip" , text.find("zip")+(indicator - 2)) > text.find('zip'):
indicator -= 2
zip_position = (text.find('zip', text.find("zip")+indicator))
print zip_position
I can see that the first code is bigger but I don't know if it's the better way to code it. And is it always better if the code is shorter? Please give me your opinion on which is a better code.
Both codes give the same results with no errors.