0

I'm writing a program to print the reverse of a given string. I am able to write the function to reverse the string but not able to take as many inputs as given in the test cases.

I've tried using "while" loop but I'm not able to get all the test cases as input. Maybe the syntax is wrong. I'm a newbie.

def rev(sample_string):
    return sample_string[::-1]

t_case = int(input())   #No. of testcases
i = 0

while i < t_case:
    sample_string = str(input(""))    #take as many inputs as no. of 
                                      #testcases 
    i =+ 1

print(rev(sample_string))

for sample input: 2, ab, aba -------- output should be: ba, aba //(in separate lines)

2
  • Possible duplicate of Trying to take size and input of a list in a single line Commented Oct 8, 2019 at 15:01
  • You're only calling rev once, outside of the loop. Move that inside of the loop and it will print the sample string you're getting from the user Commented Oct 8, 2019 at 15:19

1 Answer 1

1

If you want to save and print multiple strings, you need a datatype to do so. List would do that job:

def rev(sample_string):
    return sample_string[::-1]

t_case = int(input())   #No. of testcases
i = 0
string_list = []  # List to store the strings

while i < t_case:
    string_list.append(str(input("")))    #take as many inputs as no. of 
                                      #testcases 
    i += 1

for string in string_list:  # iterate over the list and print the reverse strings
    print(rev(string))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.