繁体   English   中英

"我不断收到错误(str'对象没有属性'append')"

[英]I keep getting ERROR (str' object has no attribute 'append')

我在行(encoded_text.append("new_letter"))<\/code>中得到 ERROR (str' object has no attribute 'append')<\/code> ) 但在(encoded_text.append("x"))<\/code>中没有,尽管encoded_text<\/code>被定义为List<\/code>类型。

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}.")

两个问题:不正确的对齐和不必要的引号:

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}.")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM