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

Asked

Viewed 199 times

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"

1 answer

2


You can ask the user to enter the amount of words to be read, for this, you can use the variable quantidade which will store the amount of words, and then set a variable i which will be incremented in a loop while and check whether i is less than quantidade, and inside the loop do the reading.

Take an example:

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

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

Entree:

3

Entree:

Palavra1

Exit:

Palavra1

Entree:

Word2

Exit:

Word2

Entree:

Word3

Exit:

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)))

Entree:

2

Entree:

egg

Entree:

bird

Exit:

The word egg eh Palindromo: YES
The word bird is Palindromo: NO

First I set a list palavras that will store the words informed by the user according to the quantity specified at the beginning of the program, then I got the amount of words that should be informed and saved in the variable quantidade, and I read the words and I kept them on the list palavras and finally made the display of all typed and validated words through the method ehPalindromo() returning SIM or 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?

  • @Guilhermesantanadesouza I edited the answer.

  • Thank you, I’ll make the changes.

  • 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.