Pour mon code de cryptage, j'essaie de renvoyer une valeur d'une fonction car elle est utilisée dans la suivante. Je continue à recevoir une erreur, me disant que le nom 'cipher_text' n'est pas défini. S'il vous plaît aider!Fonction Python - La valeur de retour est 'non définie'
Erreur:
(ligne 7)
décryptage (cipher_text, changement)
NameError: nom 'cipher_text' est pas défini
def main():
user_input = input ("Enter string: ")
shift = int(input ("Enter a shift that is between 1 and 26: "))
while shift<1 or shift>26:
shift = input ("ERROR: Shift must be between 1 and 26: ")
encryption (user_input, shift)
decryption (cipher_text, shift)
frequency (user_input)
def frequency(user_input):
freq_char = None
for char in user_input:
charcount = user_input.count(char)
if (charcount != 0):
freq_char = char
print (freq_char)
return fre_char
def encryption(user_input, shift):
cipher_text = ''
for char in user_input: #for every character in input
if char == ' ':
cipher = char
cipher_text += cipher
else:
cipher_num = (ord(char))+(shift)%26 #using ordinal to find the number
cipher= ''
cipher = chr(cipher_num)# using chr to convert back to a letter
cipher_text += cipher
print ("The encrypted text is:",cipher_text)
return(cipher_text)
def decryption (cipher_text, shift):
decrypt_text = ''
cipher_text = ''
for char in cipher_text: #for every character in the encrpted text
decrypt_num = (ord(char))+(int(shift))%26
decrypt= ''
decrypt = chr(decrypt_num)
decrypt_text += decrypt
print("The decrypted text is:", decrypt_text)
return(decrypt_text)
main()
'decryption' devrait retourner quelque chose. –
@ArtjomB. Je n'essayais pas de fournir une révision de code, plutôt pour aider à expliquer son problème. Dans ce cas, sa fonction imprimerait toujours quelque chose, et il n'utilise pas le retour, donc j'ai évité de compliquer la réponse =) – Cireo