Python Forum

Full Version: string ending with other string
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have to write a program that inputs a pair of string from user. The program should dispaly an appropriate message if either one of the strings ends with the other string. The case of the input
strings does not matter. The program should continue to enter pairs of strings until the user
enters an empty string for either.
Here are sample run:
Enter first string: Hello World

Enter second string: world
String 2 is at the end of String 1

Enter first string: abc

Enter second string: defABC
String 1 is at the end of String 2

Enter first string: hello world
Enter second string: hello

String 1 and String 2 do not end with the other

Enter first string: Hello World again
Enter second string:
second string is empty, goodbye.

Here is my code, program should exit when I put exit when I enter an empty input, butmy code fails to do it, where is my mistake?
f = str(input('Enter first string:'))
s = str(input('Enter second string:'))
while ((len(f) or len(s)) != 0) and (f[0:len(f)] == s[len(s)-(len(f)):len(s)]):
    print('String 1 is at the end of String 2')
    f = input('Enter first string:')
    s = input('Enter second string:')
    while ((len(f) or len(s)) != 0) and (s[0:len(s)] == f[len(f)-(len(s)):len(f)]):
        print('String 2 is at the end of String 1')
        f = input('Enter first string:')
        s = input('Enter second string:')
        while ((len(f) or len(s)) != 0) and (s not in f[len(f)-len(s):len(f)] and f not in s[len(s)-len(f):len(s)]):
            print('String 1 and String 2 do not end with the other')
            f = input('Enter first string:')
            s = input('Enter second string:')
else:
    print('Second string is empty, goodbye.')
Why do you have three while loops? You should have one while loop. You should check for string at the end of other strings with a conditional (if/elif/else). Exit out of the loop with a break statement if the output is empty.