我的程序一直在抛出一个语法错误,没有给出这个代码的理由,我无法弄清楚为什么我的生活。我推断出造成错误的行是我已经删除的行
#char = ord(message[i])-96
#key = ord(keyword[i])-96
它们位于代码的第15行和第16行。请帮帮我!!!
option = input("Do you want to encrypt or decrypt? (E/D): ")
keyword = input("Enter your keyword: ")
message = input("Enter your message: ")
while len(keyword)<len(message):
keyword=keyword+keyword
keyword=keyword[:len(message)]
newMessage = ""
for i in range(len(message)):
char = ord(message[i])
key = ord(keyword[i])
if char==32:
newMessage = newMessage+" "
elif char<97 or char>122:
message = input("Enter your message: ")
#char = ord(message[i])-96
#key = ord(keyword[i])-96
elif option == "E":
if char+key>26:
newMessage = newMessage+chr(char+key-26)
else:
newMessage = newMessage+chr(char+key)
else:
if char-key<1:
newMessage = newMessage+chr(char-key+26)
else:
newMessage = newMessage+chr(char-key)
print(newMessage)
答案 0 :(得分:1)
您将使用这两行结束if
和后续elif
。因此,elif option == "E":
毫无意义,因为之前没有前面的if
语句。你要么缩进:
elif char<97 or char>122:
message = input("Enter your message: ")
char = ord(message[i])-96
key = ord(keyword[i])-96
或者随后的if
elif
语句
if option == "E":
if char+key>26:
newMessage = newMessage+chr(char+key-26)
else:
newMessage = newMessage+chr(char+key)
答案 1 :(得分:0)
你没有缩进(前面放4个空格)这两行。正如你在if
陈述中那样,你必须把它们放进去。