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语句时,我得到错误消息:无效语法。我知道我想得太多了。你知道吗
我看到代码中有几个问题:
.lower
调用缺少括号(请注意,没有paren的'xyz'.lower
将始终计算为True
)print(plainText)
需要缩进elif
分支需要与它所使用的if
分支对齐code = code
什么都不做plainText
变量;相反,应该使用一个新变量,例如decodedText
相关问题 更多 >
编程相关推荐