I get ERROR (str' object has no attribute 'append')<\/code> in line
(encoded_text.append("new_letter"))<\/code> but not in
(encoded_text.append("x"))<\/code> although
encoded_text<\/code> is defined as a
List<\/code> type.
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
text = list(text)
encoded_text = []
encoded_text.append("x")
if(direction == "encode"):
for x in text:
new_index = alphabet.index(x) + shift
if(new_index > len(alphabet)):
new_index = new_index - len(alphabet)
new_letter = alphabet[new_index]
encoded_text.append(new_letter)
encoded_text= "".join(encoded_text)
print(f"The encoded text is {encoded_text}.")
Two problems: incorrect alignment and unnecessary quotes:
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
text = list(text)
encoded_text = []
encoded_text.append("x")
if(direction == "encode"):
for x in text:
new_index = alphabet.index(x) + shift
if(new_index > len(alphabet)):
new_index = new_index - len(alphabet)
new_letter = alphabet[new_index]
encoded_text.append(new_letter) # << removed quotes here
encoded_text= "".join(encoded_text) # << corrected alighment here
print(f"The encoded text is {encoded_text}.")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.