def main():
plainText = input("Enter a one-word, lowercase message: ")
distance = int(input("Enter the distance value: "))
code = ""
for ch in plainText:
ordValue = ord(ch)
cipherValue = ordValue + distance
if cipherValue > ord('z'):
cipherValue = ord('a') + distance - (ord('z') - ordValue + 1)
code += chr(cipherValue)
print(code)
userDecrypt = input("Would you like to decrypt the text? ").lower
# i want this yes answer to force the next few lines of code to run and if no then it quits the program
if userDecrypt == 'yes' or userDecrypt == 'y':
code = code
plainText = ""
for ch in code:
ordValue = ord(ch)
cipherValue = ordValue - distance
if cipherValue < ord('a'):
cipherValue = ord('z') - (distance - (ord('a') - ordValue + 1))
plainText += chr(cipherValue)
print(plainText)
# this is where my problem lies. It won't allow this
elif userDecrypt == 'no' or userDecrypt == 'n':
print("Have a good day")
main()
elif
语句是我的问题。我希望能够运行解密代码,如果用户选择是,如果没有,那么我希望程序退出。如果是的话,我可以让它解码但是当我添加else语句时,我收到错误消息:语法无效。我知道我在想这个。
答案 0 :(得分:0)
我在发布的代码中看到了几个问题:
.lower
调用缺少括号(请注意'xyz'.lower
没有parens 总是评估为True
)print(plainText)
需要缩进elif
分支需要缩进以与if
分支对齐code = code
没有做任何事情plainText
变量不应重复使用;相反,您应该使用新的变量,例如decodedText