This forum is in archive mode. You will not be able to post new content.

Author Topic: [Python] Need Help ! Not sure where i'm wrong.  (Read 426 times)

0 Members and 1 Guest are viewing this topic.

Offline wolverine

  • /dev/null
  • *
  • Posts: 6
  • Cookies: -1
  • Live,Learn,Lead !
    • View Profile
[Python] Need Help ! Not sure where i'm wrong.
« on: September 16, 2014, 05:57:03 AM »
Code: [Select]

def is_rev(word1, word2):
    if len(word1) != len(word2):
        return False
    i = 0
    j = len(word2)-1
    print(i,j)


    while j > 0:
        if word1[i] != word2[j]:
            return false
        i = i + 1
        j = j - 1
Quote
When i run this script it doesn't go through the i and j part in the while loop. I'm not sure why.
Quote
The output is like
Code: [Select]

>>> is_rev('pots', 'stop')
0 3
« Last Edit: September 16, 2014, 05:59:39 AM by wolverine »
Humanity can thrive only by co-operation not by competition

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: [Python] Need Help ! Not sure where i'm wrong.
« Reply #1 on: September 16, 2014, 06:27:19 AM »
Lets work through this nice and slow for everyone.


When you first his the while loop j equals 3 and i equals 0.
For each iteration j will decrement and i will increment
The while loop will only continue if the letter in one string coresponds with the letter in another.
So with a table (similar to my first year comp sci course...)
Code: [Select]
j|i | will run?   |   letters being compared
------------------|   p and p
3 0 | yes         }   o and o
2 1 | yes         |   t and t
1 2 | yes         |   s and s
0 3 | no

YEAH WHOOHOO so i got all the way through the loop without ever having different letts, which a prerequisite for an output...


Oh yeah. You forgot a return true statement, but I was having fun...

Sidenote: please try with racecar racecar and kayak kayak, just for the lulz


removed silly font n stuff,added code tags
« Last Edit: September 16, 2014, 11:26:22 AM by proxx »
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.