play_with_strings_reverse_me.py 776 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/python
  2. # this is a shebang: https://en.wikipedia.org/wiki/Shebang_%28Unix%29
  3. '''
  4. This script takes a sentence you write in the terminal, and gives it back in reverse mode
  5. Check out other options for list comprehension: https://docs.python.org/3/tutorial/datastructures.html
  6. Made for OLA #5, Paris, 15-17 décembre 2017
  7. '''
  8. # Run script in loop:
  9. while True:
  10. # Ask to write a sentence
  11. sentence = input("Ecrivez votre phrase: ").lower().strip('\., \?')
  12. # Split sentence into words
  13. words = sentence.split()
  14. #print(words)
  15. # if sentence is only 1 word, reverse word
  16. if len(words) < 2:
  17. for word in words:
  18. word = word[::-1]
  19. print(word.capitalize())
  20. # if sentence is more than 1 word
  21. else:
  22. words.reverse()
  23. print(" ".join(words).capitalize(), '.')