简体   繁体   English

函数中的返回值在 python 中不起作用

[英]Return Value in a function not working in python

I need to create a ceasar cipher program that takes a string, converts it into ASCII, shifts it by 4, converts it back into a string, prints it, then deciphers it by reversing the cipher function, then prints the original string.我需要创建一个 ceasar 密码程序,它接受一个字符串,将其转换为 ASCII,将其移动 4,将其转换回字符串,打印它,然后通过反转密码函数对其进行解密,然后打印原始字符串。 However, The return value in my functions aren't working.但是,我的函数中的返回值不起作用。 How do I fix this?我该如何解决? #cipher function #密码函数

def cipher(string):
    string = ""
    NewX = 0
    encrypted = 0
    for letter in range(len(string)):
        NewX = string[letter]
        #encrypted is the encrypted word 
        encrypted = chr(ord(NewX)+4)
    return encrypted


#Decipher function
def decipher(NewX):
   NewString  = ""
   for letter in range(len(encrypted)):
       NewString = chr(ord(encrypted)-4)

    return(NewString)


def main():
    string = input("enter your string here: ")
    cipher(string)
    print(encrypted)
    decipher(NewX)
    print()


main()

You would need to store the values the function is returning in a variable.您需要将函数返回的值存储在变量中。 For example, in your main() function block例如,在您的main()功能块中

encrypted= cipher(string)
print(encrypted) # Printing stored value
print(decipher(NewX)) # You can directly print it too without storing

There are quite a few errors in the code.代码中有不少错误。

Indentation.缩进。 In Python, blocks of code are indicated by how much the code is indented.在 Python 中,代码块由代码缩进的程度来表示。 In the decipher function,在解密函数中,

 NewString  = ""
 for letter in range(len(encrypted)):
     NewString = chr(ord(encrypted)-4)
  return(NewString) 

The return is one space too far to the right, or the lines above are one space too far to the left (more likely). return是向右太远一个空格,或者上面的行是向左太远一个空格(更有可能)。

Concatenation.级联。 In the code, the result is declared to be empty, 0 or "" (and setting String to "" blanks whatever string you input) .在代码中,结果被声明为空、0 或 ""(并且将String设置为 "" 会使您输入的任何字符串变为空白)。 Then the idea, clearly, is to add to the end of the result the converted character.显然,这个想法是将转换后的字符添加到结果的末尾。 However, the code assigns the converted character instead, and over-writes whatever is there instead.但是,代码分配了转换后的字符,并改写了那里的任何内容。 A simple approach is to concatenate with the operator +=一种简单的方法是与运算符 += 连接

encrypted += chr(ord(NewX)+4)

Parameters.参数。 In the decipher function, the parameter passed in is NewX , but the variable actually used is encrypted .在 decipher 函数中,传入的参数是NewX ,但实际使用的变量是encrypted So this is the parameter that should have been passed in. Also, you're running ord on the whole string, not on individual characters.所以这是应该传入的参数。此外,您是在整个字符串上运行ord ,而不是在单个字符上运行。

For loops.对于循环。 You can simply say:你可以简单地说:

for letter in string:

rather than而不是

for letter in range(len(string)):
    NewX = string[letter]

Return values.返回值。 In the code, running the encypher and decipher functions are run, but since the results are not captured the results are thrown away.在代码中,运行加密和解密函数,但由于未捕获结果,因此结果被丢弃。

With these errors corrected, the code is:纠正这些错误后,代码为:

def cipher(string):
    encrypted = ""
    for letter in string:
        #encrypted is the encrypted word 
        encrypted += chr(ord(NewX)+4)
    return encrypted


#Decipher function
def decipher(encrypted):
    NewString  = ""
    for letter in encrypted:
        NewString += chr(ord(letter)-4)

    return(NewString)


 def main():
     string = input("enter your string here: ")
     encrypted=cipher(string)
     print(encrypted)
     decyphered=decipher(encrypted)
     print(decyphered)

 main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM