I decided to step out of my comfort zone (Java) and try out Python. I wrote a very simple reverser.py and want it to be reviewed.
"""
This python script basically takes a string and reverses it.
This python script can be used as a command line script as well as a separate
module.
"""
import sys
def reverse(message):
"""
This function takes a string and reverses it, so as to somewhat obfuscate
it.
:param message: The unencrypted text message to be reversed.
"""
text = ''
for character in message:
text = character + text
return text
if __name__ == '__main__':
numberOfArgs = len(sys.argv)
message = ''
if numberOfArgs == 1: # i.e. the program name only
message = input('Please enter the text to be encrypted : ')
elif numberOfArgs == 2:
message = sys.argv[1]
else:
print('Incorrect number of arguments : %d\n' % numberOfArgs)
print('Usage: python3 reverseCipher [unencrypted string]')
exit(2) # '2' stands for command-line syntax errors in unix
print(reverse(message))
Examples in the terminal:
$ python3 reverser.py
Please enter the text to be encrypted : I understand that text manipulation in python is quite easy.
.ysae etiuq si nohtyp ni noitalupinam txet taht dnatsrednu I
$ python3 reverser.py "I am just starting out with python"
nohtyp htiw tuo gnitrats tsuj ma I
It would be great if I would be advised about:
- Code style and idioms
- The required mindset (for I have one for Java)
- Anything that can help me learn the language