为什么我得到 `none` 作为输出?
Why am I getting `none` as the output?
我正在做作业的凯撒密码加密代码。我想我输入了我需要写的所有内容。
但是我确实有一个问题。
我在代码中得到 none
:
def fancy_caesar(message, keyword, true_false_statement):
count = 0
while count == 0:
true_false_statement = str(input("Type True if you want to encrypt.\nType false if you want to decrypt: "))
if true_false_statement == 'true' or true_false_statement == 'True' or true_false_statement == 't' or true_false_statement == 'T':
count = count + 1
elif true_false_statement == 'false' or true_false_statement == 'False' or true_false_statement == 'F' or true_false_statement == 'f':
count = count + 1
else:
print('None')
alphabet = 'abcdefghijklmnopqrstuvwxyz'
translated_message = ""
keyword_index = 0
for character in message:
if character in alphabet:
number = alphabet.find(character)
if true_false_statement == 'true' or true_false_statement == 'True' or true_false_statement == 't' or true_false_statement == 'T':
number = number + (ord(keyword[keyword_index]) - ord('a'))
elif true_false_statement == 'false' or true_false_statement == 'False' or true_false_statement == 'F' or true_false_statement == 'f':
number = number - (ord(keyword[keyword_index])) - ord('a')
keyword_index += 1
keyword_index = keyword_index % len(keyword)
if number >= len(alphabet):
number = number - len(alphabet)
elif number < 0:
number = number + len(alphabet)
translated_message = translated_message + alphabet[number]
else:
translated_message = translated_message + character
return translated_message
def main():
message = input("Enter string you want to encrypt/decrypt: ")
keyword = input('Keyword for encryption: ')
true_false_statement = ''
translated_message = fancy_caesar(message, keyword,true_false_statement)
print(translated_message)
main()
当我输入 message
输入 'sloth,' 时,我得到 none
而不是 'tlruh'。
有什么办法可以解决这个问题?
这是因为你在else
块中添加了return translated_message
(def main()
上面的语句)。
移动语句与for
语句对齐,你会得到想要的输出。
我正在做作业的凯撒密码加密代码。我想我输入了我需要写的所有内容。 但是我确实有一个问题。
我在代码中得到 none
:
def fancy_caesar(message, keyword, true_false_statement):
count = 0
while count == 0:
true_false_statement = str(input("Type True if you want to encrypt.\nType false if you want to decrypt: "))
if true_false_statement == 'true' or true_false_statement == 'True' or true_false_statement == 't' or true_false_statement == 'T':
count = count + 1
elif true_false_statement == 'false' or true_false_statement == 'False' or true_false_statement == 'F' or true_false_statement == 'f':
count = count + 1
else:
print('None')
alphabet = 'abcdefghijklmnopqrstuvwxyz'
translated_message = ""
keyword_index = 0
for character in message:
if character in alphabet:
number = alphabet.find(character)
if true_false_statement == 'true' or true_false_statement == 'True' or true_false_statement == 't' or true_false_statement == 'T':
number = number + (ord(keyword[keyword_index]) - ord('a'))
elif true_false_statement == 'false' or true_false_statement == 'False' or true_false_statement == 'F' or true_false_statement == 'f':
number = number - (ord(keyword[keyword_index])) - ord('a')
keyword_index += 1
keyword_index = keyword_index % len(keyword)
if number >= len(alphabet):
number = number - len(alphabet)
elif number < 0:
number = number + len(alphabet)
translated_message = translated_message + alphabet[number]
else:
translated_message = translated_message + character
return translated_message
def main():
message = input("Enter string you want to encrypt/decrypt: ")
keyword = input('Keyword for encryption: ')
true_false_statement = ''
translated_message = fancy_caesar(message, keyword,true_false_statement)
print(translated_message)
main()
当我输入 message
输入 'sloth,' 时,我得到 none
而不是 'tlruh'。
有什么办法可以解决这个问题?
这是因为你在else
块中添加了return translated_message
(def main()
上面的语句)。
移动语句与for
语句对齐,你会得到想要的输出。