How to supply a specific amount of strings to be tested?

1

I can test whether a user supplied string is a palindrome or not (word or phrase that can be read backwards ignoring spaces and uppercase and lowercase letters, for example: Help me get on the bus in Morocco), using the code:

string = raw_input()
stringSemEspacos = string.replace(' ', '')
stringTodaMinuscula = stringSemEspacos.lower()
stringInvertida = stringTodaMinuscula[::-1]
if stringInvertida == stringTodaMinuscula:
    print "SIM"
else:
    print "NAO

I need to write a program that, before receiving the strings to be tested, first receives an integer, which corresponds to the number of strings that must be tested. If you want to test 4 strings, the number 4 must be the first entry in the program, followed by the 4 strings that will be tested, and the program must judge each string and print the 4 answers, between Yes and No. How to do it receive the amount of strings established, make the judgment and only then be terminated?

How do I do:

i = 0
quantidade = int(raw_input())
while i < quantidade:
    i += 1
    string = raw_input()
    stringSemEspacos = string.replace(' ', '')
    stringTodaMinuscula = stringSemEspacos.lower()
    stringInvertida = stringTodaMinuscula[::-1]
    if stringInvertida == stringTodaMinuscula:
        print "SIM"
    else:
        print "NAO"
    
asked by anonymous 28.03.2016 / 23:58

1 answer

2

You can ask the user to enter the number of words to be read, so you can use the quantidade variable that will store the number of words, and then define a i variable that will be incremented by one loop while and check that i is less than quantidade , and inside the read loop.

See an example:

i = 0
quantidade = input("Quaintidade de palavras a seram lidas: ")

while (i < quantidade):
    palavra = raw_input("Palavra: ")
    print (palavra)
    i += 1

Entry:

  

3

Entry:

  

Word1

Output:

  

Word1

Entry:

  

Word2

Output:

  

Word2

Entry:

  

Word3

Output:

  

Word3

See the adaptation to solve the word verification problem.

Code:

def ehPalindromo(palavra):
    stringSemEspacos = palavra.replace(' ', '')
    stringTodaMinuscula = stringSemEspacos.lower()
    stringInvertida = stringTodaMinuscula[::-1]

    if stringInvertida == stringTodaMinuscula: return "SIM"
    else: return "NAO"

i = 0
palavras = []
quantidade = input("Quaintidade de palavras a seram lidas: ")

while (i < quantidade):
    palavras.append(raw_input("Palavra: "))
    i += 1

for p in palavras:
    print("A palavra {} eh Palindromo: {}".format(p, ehPalindromo(p)))

Entry:

  

2

Entry:

  

egg

Entry:

  

bird

Output:

  

The word egg eh Palindromo: YES
  The word ave eh Palindromo: NAO

First of all I define a palavras list that will save the words entered by the user according to the amount specified at the beginning of the program, then I get the amount of words that must be informed and saved in the variable quantidade , and I read the words and saved them in the palavras list and finally I showed all typed and validated words using the ehPalindromo() method that returns SIM or NAO .

    
29.03.2016 / 00:10