简体   繁体   中英

return strings in alphabetical order

I was given a problem for a beginners Python class to display 3 strings in alphabetical order using only what we have learned so far. We haven't covered lists or sort().

This is what I have so far:

print("Instructions: Please enter three strings.")
string1 = input("Enter the first string: ")
string2 = input("Enter the second string: ")
string3 = input("Enter the third string: ")

string1_lower = string1.lower()
string2_lower = string2.lower()
string3_lower = string3.lower()

string1_upper = string1.upper()
string2_upper = string2.upper()
string3_upper = string3.upper()

if string1_lower < string2_lower < string3_lower:
    print(string1)
    print(string2)
    print(string3)

elif string1_lower < string3_lower < string2_lower: 
    print(string1)
    print(string3)
    print(string2)

elif string2_lower < string1_lower  < string3_lower:
    print(string2)
    print(string1)
    print(string3)

elif string2_lower < string3_lower < string1_lower:
    print(string2)
    print(string3)
    print(string1)

elif string3_lower < string1_lower < string2_lower:
    print(string3)
    print(string1)
    print(string2)

else:
    print(string3)
    print(string2)
    print(string1)

This works for lowercase strings, but how would i make it work with a string that starts with a capital letter?

eg

car boxcar Boxcar ---> car Boxcar boxcar

If you want to sort based on the strings as entered, that's simple: just don't use the lower-cased variants for comparison - use the original strings instead.

However, you also have another problem in that, if two of the (lower-cased) strings are identical and the third is different, it will always end up in the else block (a) .

To fix that, you should be using <= rather than < . I suspect that's a more serious problem here since it will map, for example:

boxcar Boxcar car

into something that's clearly not alpha order:

car Boxcar boxcar

Although you state that you're only allowed to use the stuff you've learned to date, there are better ways to do what you want. It won't help with this specific question but may make it a lot easier in future, if you use features available in the most recent incarnation of the language:

strings = []
while (inputStr := input("Enter string (empty string to stop)> ")) != "":
    strings.append(inputStr)

print("Sorted, case-insensitive:")
for str in sorted(strings, key = str.lower):
    print(f"   {str}")

That's substantially shorter than your current code, handles arbitrary numbers of input strings, and uses built-in sorting functionality, including allowing you to easily change how the sorting is done. A sample run is shown below:

Enter string (empty string means stop)> boxcar
Enter string (empty string means stop)> CaR
Enter string (empty string means stop)> bOxCaR
Enter string (empty string means stop)> car
Enter string (empty string means stop)> CAR
Enter string (empty string means stop)> BOXCAR
Enter string (empty string means stop)> 
Sorted, case-insensitive:
   boxcar
   bOxCaR
   BOXCAR
   CaR
   car
   CAR

(a) Actually, it will do that even for three identical strings but, in that case, it won't matter what order they get printed in.

I realize that you're a beginner, but you're doing a lot of work that you should be letting the computer do. You should be thinking of how to do things so that, for example, it doesn't matter how many strings you want to display and sort. Here's something just to show you where you're headed if you keep learning...

# Print instructions for the user
print("Instructions: Please enter A string (just hit return when done")

# An empty list we'll put our entered strings in
strings = []

while True:
    # Have the user enter a string
    s = input('>')

    # If the string is empty, then we're done accepting strings
    if s == '':
        break

    # Add the entered string to the list
    strings.append(s)

# sort the list of strings such that case matters
strings = sorted(strings)

# print the list
print("sorted where case matters...")
for s in strings:
    print(s)

# sort the list of strings ignoring case
strings = sorted(strings, key=str.lower)

# print the list
print("sorted ignoring case...")
for s in strings:
    print(s)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM