任何指導表示贊賞。我是編程新手。問題:“ str不可調用”的運行時錯誤。并且,可能是語義錯誤。詳細信息:“如果len <= 70:TypeError:'str'對象不可調用”預期的結果:我正在嘗試編寫一個函數,該函數接受一個字符串,然后打印該字符串,以便該字符串的最后一個字母在顯示的第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)
3 回答
MYYA
TA貢獻1868條經驗 獲得超4個贊
如果我理解您的要求,則希望在字符串前面放置足夠的空格,以便其最后一個字符在第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)
添加回答
舉報
0/150
提交
取消
