commit c5377194dfdfdec4d99bc514b24ef62ab26beef4 (make_pythonic) Date: Thu Feb 17 11:15:48 2022 -0500 Seek forgiveness, not permission It is more Pythonic (and safer in concurrent situations) to attempt an operation and handle failure, rather than trying to check that the operation should succeed first. This is a non-functional style change. diff --git a/python/scrambler.py b/python/scrambler.py index 0f5e989..5fe6148 100644 --- a/python/scrambler.py +++ b/python/scrambler.py @@ -4,12 +4,14 @@ import sys from cs5150.cipher_string import CipherString if __name__ == '__main__': - if len(sys.argv) != 2: + try: + plaintext = sys.argv[1] + except IndexError: print(f'Invalid number of arguments ({len(sys.argv) - 1} != 1)', file=sys.stderr) raise SystemExit(f'Usage: {sys.argv[0]} ') try: - p = CipherString.from_string(sys.argv[1].upper()) + p = CipherString.from_string(plaintext.upper()) k = CipherString.from_string('cdm89'.upper()) print(k.encrypt(p)) except ValueError: