Python 3中的if函数错误
我在大学计算机编程课的一次练习中遇到了python中if函数的问题,并且在下面的程序中遇到了第二个if语句的问题。它说有一个语法错误:Python 3中的if函数错误,python,if-statement,syntax-error,caesar-cipher,Python,If Statement,Syntax Error,Caesar Cipher,我在大学计算机编程课的一次练习中遇到了python中if函数的问题,并且在下面的程序中遇到了第二个if语句的问题。它说有一个语法错误: myPlaintext = input("enter plaintext: ") 距离=输入端距离: def纯文本,距离: for ch in myPlaintext: cipherText = '' if ch.isalpha(): stayinAlphabet = ord(ch) + distance if
myPlaintext = input("enter plaintext: ")
距离=输入端距离:
def纯文本,距离:
for ch in myPlaintext:
cipherText = ''
if ch.isalpha():
stayinAlphabet = ord(ch) + distance
if stayinAlphabet <= 146:
stayinAlphabet -= 26
else:
stayinAlphabet = (stayinAlphabet-121)%26+97
final_letter = chr(stayinAlphabet)
cipherText += final_letter
print (cipherText)
return (cipherText)
这是一个新函数,我已经修复了错误。当我使用任何字母作为输入时,该函数起作用。然而,当我输入一个空格时,在读取cipherText+=final_字母的行上出现错误,并表示final_字母在赋值之前被引用。一如既往,谢谢你的帮助
任务是编写一个使用凯撒密码加密消息的代码。谢谢,如果还有其他错误,请告诉我,因为我是python新手,很容易出错您的缩进不一致。记下函数的起始位置,然后记下函数体的起始位置。该函数下方的所有内容(最后一个除外)都需要在右侧缩进4个空格。因此:
def caesar(my_text , distance):
for ch in my_text:
if ch.isalpha():
alphabet = ord(ch) + distance
if alphabet < 26:
alphabet -=26
else:
alphabet = (alphabet)%26
final_letter = chr(alphabet)
ciphertext = ""
ciphertext += final_letter
print (("cipher text is: ") , ciphertext)
return (ciphertext)
编辑:函数声明后的冒号丢失,print语句的右括号丢失。def语句和check intendation后的“:”位置有语法和缩进错误。请在问题中发布回溯。
def caesar(my_text , distance):
for ch in my_text:
if ch.isalpha():
alphabet = ord(ch) + distance
if alphabet < 26:
alphabet -=26
else:
alphabet = (alphabet)%26
final_letter = chr(alphabet)
ciphertext = ""
ciphertext += final_letter
print (("cipher text is: ") , ciphertext)
return (ciphertext)