I need to print a list of words inputed by the user from the command line. Now, the alplhabetical order works fine but when I print in reverse it does not come out right. I tried a lot of things and I am now out of ideas. Anyone? Here is the code:
import argparse
argparser = argparse.ArgumentParser()
argparser.add_argument("user_string", help = 'User string')
argparser.add_argument("--reverse", "-r", action="store_true", dest="reverseflag")
args = argparser.parse_args()
user_string = args.user_string
words_to_sort = user_string.split()
if len(words_to_sort) < 2:
args.user_string
# alerts user to input more than 1 word
print("Invalid command line arguments to program. Please, supply two or more strings to sort.")
if len(words_to_sort) > 1 and (args.reverseflag == True):
words_to_sort = sorted(args.user_string, reverse=True)
print(*words_to_sort)
else:
words_to_sort.sort()
for word in words_to_sort:
print(word)
this is what I get from the command line:
PS C:\Users\desktop\folder> python mysort.py --reverse "all mall ball"
m l l l l l l b a a a
PS C:\Users\desktop\folder> python mysort.py "all mall ball"
all
ball
mall
The reverse should just reverse the array from z to a but sadly it isn't the case.