while_input_abecedaire~20171216-073018.py 888 B

123456789101112131415161718192021222324252627282930
  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. A list is ordered and changeable. It is less efficient than a set/tuple. But it allows to work with index numbers.
  6. Check out other options for list comprehension: https://docs.python.org/3/tutorial/datastructures.html
  7. Made for OLA #5, Paris, 15-17 décembre 2017
  8. '''
  9. # Run script in loop:
  10. while True:
  11. # Ask to write a sentence
  12. sentence = input("Ecrivez votre phrase: ").lower().strip('\., \?')
  13. # Split sentence into words
  14. words = sentence.split()
  15. #print(words)
  16. # if sentence is only 1 word, reverse word
  17. if len(words) < 2:
  18. for word in words:
  19. word = word[::-1]
  20. print(word.capitalize())
  21. # if sentence is more than 1 word
  22. else:
  23. words.sort()
  24. print(" ".join(words).capitalize(), '.')