任何指导都是值得感谢的。我是编程新手。
问题:'str is not callable.‘’的运行时错误。而且,很可能是语义错误。说明:"if len(s) str 70: TypeError:‘<=’object is not callable“
预期结果:我正在尝试编写一个函数,该函数接受一个字符串,然后打印该字符串,使该字符串的最后一个字母位于显示的第70列。
我尝试过的:在PEP8中运行代码,它不会返回任何语法错误。删除了将str分配给% s的原始行。
Python 3中的代码:
def right_justify(s):
'''
(string) -> string
takes a string named s
places it in column 70 - len of string
'''
s = input("Type in a word: ")
for len in s:
if len(s) <= 70:
len(s) + 70 - len(s)
return s
print(right_justify)
发布于 2018-07-09 02:19:33
如果我理解您的要求,您希望在字符串前面放置足够的空格,以便它的最后一个字符位于第70列。
您不需要遍历字符串。这将实现您所需的功能。注意,没有必要将input
放在函数中,因为您不能使用字符串参数调用函数。
def right_justify(s):
'''
(string) -> string
takes a string named s
places it in column 70 - len of string
'''
if len(s) <= 70:
output = ((70 - len(s)) * " ") + s
return output
input_string = input("Type in a word: ")
output_string = right_justify(input_string)
print(output_string)
发布于 2018-07-09 02:41:34
最简单的方法是:
def right_justify(s):
return "%70s" % s
https://stackoverflow.com/questions/51234746
复制相似问题