debugpythonMinor
If a message is there, but you cannot see it, is it really there?
Viewed 0 times
cannotreallyyoumessagebutseethere
Problem
I have created a space encoder program, because it is fun to generate invisible messages. (This programme is not cryptographically secure, it is just for fun)
import doctest
def to_spaces(text):
"""
Each char with ASCII code x gets converted to a
line of x spaces.
>>> to_spaces('a').count(' ')
97
>>> to_spaces("hello").count(' ')
532
"""
return '\n'.join((' '*ord(char) for char in text))
def from_spaces(spaces):
"""
Each line of x spaces gets converted to the
char of ASCII code x.
>>> from_spaces(' '*97)
'a'
"""
return ''.join((chr(len(i)) for i in spaces.splitlines()))
def encode_and_print_file():
filename = input("Where do you want to save the encrypted text? ")
content = input("Enter your secret message: ")
with open(filename,"w+") as f:
f.write(to_spaces(content))
def decode_and_print_file():
filename = input("Which file do you want to read decoded? ")
with open(filename) as f:
print(from_spaces(f.read()))
def user_interface():
while True:
choice = input("\nDo you want to encode or decode? ")
encode_and_print_file() if 'en' in choice.lower() \
else decode_and_print_file()
if __name__ == "__main__":
doctest.testmod()
user_interface()Solution
-
Coding style looks good except for one thing. You are using a conditional expression where an
As the value of the expression is not used, I would prefer the statement version as it is clearer:
-
I don't see why you use
Coding style looks good except for one thing. You are using a conditional expression where an
if statement would do.encode_and_print_file() if 'en' in choice.lower() \
else decode_and_print_file()As the value of the expression is not used, I would prefer the statement version as it is clearer:
if 'en' in choice.lower():
encode_and_print_file()
else:
decode_and_print_file()-
I don't see why you use
if 'en' in choice.lower() to recognize the word "encode", allowing "en" anywhere. if choice.strip().lower().startswith('en') would be more to the point.Code Snippets
encode_and_print_file() if 'en' in choice.lower() \
else decode_and_print_file()if 'en' in choice.lower():
encode_and_print_file()
else:
decode_and_print_file()Context
StackExchange Code Review Q#82412, answer score: 2
Revisions (0)
No revisions yet.