[英]“Can't assign to function call” error
My function is supposed to take a string argument as input, and return the rot-13 encoding of the input string. 我的函数应该将字符串参数作为输入,并返回输入字符串的rot-13编码。
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. ord
是内置函数。 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. 这可能不会解决您的问题,因为您还有其他错误,例如您尝试在不声明变量z
情况下添加到变量z
。 You should declare it before your for loop: 您应该在for循环之前声明它:
z = ''
for num in c:
...
and also indent the line 并缩进线
z += chr(ord(num))
so that it is inside the for loop. 使其位于 for循环内。 You can also make it: 您也可以做到:
z += num
as chr and ord are reverse functions. 因为chr和ord是反向函数。
What you're doing wrong is, you're assigning to a function call! 您在做错的是,您正在分配给函数调用! Eg: 例如:
ord(num) -=13
you're assigning to the function call ord(num)
-- and, you can't do it. 您将分配给函数调用ord(num)
,并且您无法执行此操作。
What you actually want to do is presumably: 您实际上想做的是:
num = chr(ord(num) - 13)
and so on. 等等。
Of course, you'll still have problems appending to z
unless you define z
in a part of the code you chose not to show us. 当然,除非在您选择不显示给我们的代码的一部分中定义z
,否则附加到z
仍然会有问题。 Hard to help debug code you choose to hide from us, of course. 当然,很难帮助调试您选择对我们隐藏的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.