Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop issues
#4
Alright, let's go without enumerate, then. What we really want, is an index. That way, we can compare characters from both strings, that are at the same position in the string. The first character of each string, then the second character of each, then the third, etc.

Does this make more sense?
>>> first = 'socks'
>>> second= 'laces'
>>> for ndx in range(len(first)):
...   left = first[ndx]
...   right = second[ndx]
...   print("Index we're comparing: {0}".format(ndx))
...   print("Character of first string at that index: {0}".format(left))
...   print("Character of second string at that index: {0}".format(right))
...   print("Are they the same? {0}".format(left == right))
...   print(" -- ")
...
Index we're comparing: 0
Character of first string at that index: s
Character of second string at that index: l
Are they the same? False
 --
Index we're comparing: 1
Character of first string at that index: o
Character of second string at that index: a
Are they the same? False
 --
Index we're comparing: 2
Character of first string at that index: c
Character of second string at that index: c
Are they the same? True
 --
Index we're comparing: 3
Character of first string at that index: k
Character of second string at that index: e
Are they the same? False
 --
Index we're comparing: 4
Character of first string at that index: s
Character of second string at that index: s
Are they the same? True
 --
Reply


Messages In This Thread
For loop issues - by BigEasy - Feb-12-2018, 04:29 PM
RE: For loop issues - by nilamo - Feb-12-2018, 05:08 PM
RE: For loop issues - by BigEasy - Feb-12-2018, 05:52 PM
RE: For loop issues - by nilamo - Feb-12-2018, 06:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Having issues getting a loop to work PLESSE HELP ASAP manthus007 1 2,185 Aug-25-2018, 10:44 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020