Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For-loops in Python 3.0

I am currently working on a palindrome-detector (anna, lol, hahah etc) and I am requested to use for-loops.

I want the program to loop through two strings (read them regularly and backwards at the same time while comparing the values). If the values are the same then the palindrome is True; if not, it's False.

My question to you is: how do you get two for-loops to run simultaneously and compare the values of the strings?

Currently doing something like this: (Python 3.0), can post entire code if needed:

   palindrom = True
text2 = ("")
for i in nytext:
    for i in nytext[::-1]:
        text2 = (nytext[::-1] + i)
        if text2 == nytext:
            palindrom = True
        else:
            palindrom = False
return palindrom

Thank you for your help!

EDIT: I might not have been clear enough when describing the problem. The program does this: It lets the user enter a string of text (such as hello my name is lol) and the program is designed to see if this is a palindrome. It is divided into three functions (and a main function).

Function number 1 fixes the text so it is reduced into characters and digits only (so LOL,,,,,, becomes lol for easier reading). Function number 2 is designed to test (using for-loops(!)) if the input is a palindrome. Function number 3 is simply going to post whether it is a palindrome or not.

I have to use for-loops for this and I cannot simply do a comparison such as: backwardtext = text.reverse() if backwardtext == text: print ("It is a palindrome")

I hope this clears things up.

like image 284
user1916173 Avatar asked Dec 20 '12 15:12

user1916173


People also ask

How do you write a loop in Python 3?

To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

What are the 3 types of loops?

Loops are control structures used to repeat a given section of code a certain number of times or until a particular condition is met. Visual Basic has three main types of loops: for.. next loops, do loops and while loops.

What are the 3 parts of a for loop in Python?

Similar to a While loop, a For loop consists of three parts: the keyword For that starts the loop, the condition being tested, and the EndFor keyword that terminates the loop.


1 Answers

You use zip

s = 'hannah'

for c_forward,c_backward in zip(s,s[::-1]):
    ...

Maybe a slightly lower level approach would be to loop over the indices (provided your items are indexible):

for i in range(len(s)):
    c_forward = s[i]        #character as you loop going forward
    c_backward = s[-(i+1)]  #character as you loop going backward
    pass #TODO: determine if palindome ... :-p

For future visitors, these don't address all the constraints in the problem, but the easiest way to check if a string is a palindrome in python is to simply do:

def ispal(s):
    return s == s[::-1]
like image 168
mgilson Avatar answered Nov 02 '22 15:11

mgilson