“无法分配给函数调用”错误 [英] "Can't assign to function call" error
问题描述
我的函数应该将一个字符串参数作为输入,并返回输入字符串的rot-13编码。
def str_rot_13(字符串):
c =列表(字符串)
用于c中的数字:
如果ord(num)> = ord('a' )和ord('z'):
如果ord(num)> =('m'):
ord(num) - = 13
else:
ord (num)= ord('A')和ord('Z'):
如果ord(num)> =('M' '):
ord(num) - = 13
else:
ord(num)+ = 13
z + = chr(ord(num))
return z
它给了我一个无法分配给函数调用的错误。我不确定我做错了什么。
编辑:终于让它起作用了!
$ b
解决方案:
if ord(num)> ; = ord('a')和ord('z'):
如果ord(num)> =('m'):
k + = chr(ord(num)-13)
else:
k + = chr(ord(num)+13)
elif ord(num)> = ord('A')and ord('Z'):
如果ord(num)> =('M'):
k + = chr(ord(num)-13)
else:
k + = chr(ord(num )+13)
return k
ord(num) - = 13
ord
是一个内置函数。您可以使用函数返回的值,但不能为函数赋值。
您可以做的是:
num = chr(ord(num) - 13)
这可能不会解决你的问题,因为你有其他的错误,比如你试图添加到变量 z
而没有声明它的位置。你应该在for循环之前声明它:
z =''
在c中的数字:
...
并缩进行
z + = chr(ord(num))
所以它在里面 for循环。你也可以这样做:
z + = num
,因为chr和ord是反向函数。
My function is supposed to take a string argument as input, and return the rot-13 encoding of the input string.
def str_rot_13(string):
c = list(string)
for num in c:
if ord(num) >= ord('a') and ord('z'):
if ord(num) >=('m'):
ord(num) -=13
else:
ord(num) +=13
elif ord(num) >= ord('A') and ord('Z'):
if ord(num) >=('M'):
ord(num) -=13
else:
ord(num) +=13
z += chr(ord(num))
return z
It's giving me a "can't assign to function call" error. I'm not sure what I'm doing wrong.
Edit: Finally got it to work! Thanks.
The solution:
if ord(num) >= ord('a') and ord('z'):
if ord(num) >=('m'):
k+= chr(ord(num)-13)
else:
k+= chr(ord(num)+13)
elif ord(num) >= ord('A') and ord('Z'):
if ord(num) >=('M'):
k+= chr(ord(num)-13)
else:
k+= chr(ord(num)+13)
return k
The problem is with lines like this one:
ord(num) -=13
ord
is a built-in function. You can use a value returned by a function, but not assign a value to a function.
What you can do instead is:
num = chr(ord(num) - 13)
This will probably not solve your problem, as you have other bugs, like you are trying to add to variable z
without declaring it somewhere. You should declare it before your for loop:
z = ''
for num in c:
...
and also indent the line
z += chr(ord(num))
so that it is inside the for loop. You can also make it:
z += num
as chr and ord are reverse functions.
这篇关于“无法分配给函数调用”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!