Exercise
Main Function Parameters And Reversal
Objective
Develop a Python program named "reverse" that takes multiple words from the command line and prints them in reverse order. For example:
reverse one two three three two one
Example Python Exercise
Show Python Code
import sys
# Define the reverse function
def reverse():
# Get the arguments from the command line (skipping the script name)
words = sys.argv[1:]
# Reverse the list of words
reversed_words = words[::-1]
# Print the reversed words
print(" ".join(reversed_words))
# Main function to run the program
if __name__ == "__main__":
reverse()
Output
python reverse.py one two three
three two one
Share this Python Exercise