1
I can test whether a user-provided string is a palindrome or not (word or phrase that can be read backwards by ignoring upper and lower case spaces and letters such as: 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 number, which corresponds to the number of strings to be tested. If the user wants 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 responses, between Yes and No. How to make the program receive the amount of strings established, make the judgment and only then be terminated?
Like I’m doing:
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"
It works, but the result of the trial must be printed only at the end, that is, after all the strings have been provided. How to proceed?
– Guilherme Santana De Souza
@Guilhermesantanadesouza I edited the answer.
– gato
Thank you, I’ll make the changes.
– Guilherme Santana De Souza
I noticed that you used the append() function. I haven’t studied it yet, but I’ll do a read to understand. Thank you very much!
– Guilherme Santana De Souza