top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

For next loops in Python?

0 votes
22 views
x = range (5)
y = range (5)
for ply in x:
 for com in y:
 if ply==com:
 result="Tie"
 print(ply,com,result)

Why is ply always equal to com?

0 0 Tie
0 1 Tie
0 2 Tie
0 3 Tie
0 4 Tie
1 0 Tie
1 1 Tie
1 2 Tie
1 3 Tie
1 4 Tie
2 0 Tie
2 1 Tie
2 2 Tie
2 3 Tie
2 4 Tie
3 0 Tie
3 1 Tie
3 2 Tie
3 3 Tie
3 4 Tie
4 0 Tie
4 1 Tie
4 2 Tie
4 3 Tie
4 4 Tie
posted Jul 23 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

Similar Questions
0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 try:
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
 break

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..

+1 vote

Here is something I found curious about python loops.

This loop run each character in a string

def avoids(word,letters):
 flag = True
 for letter in letters:
 if(letter in word):
 flag = False
 return flag

The loop below (at the bottom) runs each line of the file

fin = open('wordplay.txt');
user_input = raw_input('Enter some characters: ')
count = 0
for line in fin:
 word = line.strip()
 if(avoids(word, user_input)):
 count += 1;

This is just too convenient. Basically my question is: Why is python not treating the contents of wordplay.txt as one long string and looping each character?

Any comment is greatly appreciate.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...