2. Crypto¶
Data file:
message.txt
Read the file "message.txt"
and write the file " crypto_en.txt"
which
should have the same text as the first file, but with vowels replaced by
"*"
.
15
text = open("message.txt", "r")
output = open(" crypto_en.txt", "w")
for line in text.readlines():
for letter in line:
if letter in "aeiouAEIOU":
output.write("*")
else:
output.write(letter)
text.close()
output.close()
Activity: 2.2 ActiveCode (ac_l23_2a_en)
Data file:
crypto_en.txt
2.1. Exercise¶
As you can see, the previous code did not change some vowels
in the file "message.txt"
. This is due to capital letters or accents.
Your task is to modify the previous program so that it changes ALL vowels
to "*"
. Remember: the .lower()
method returns a string
with all its characters turned into lowercase letters. This time, you will write to
the file " crypto2_en.txt"
.
17
text = open("message.txt", "r")
output = open(" crypto2_en.txt", "w")
# Modify the program
for line in text.readlines():
for letter in line:
if letter in "aeiou":
output.write("*")
else:
output.write(letter)
text.close()
output.close()
Activity: 2.1.1 ActiveCode (ac_l23_2b_en)
Data file:
crypto2_en.txt
You have attempted 1 of 4 activities on this page