このコードは2と9の倍数を0〜500の数字から抜き取り、それをリスト化して抜き取った数字をすべて足すようにできています(自分の中では笑)。しかし全く動きません。何を試したらいいかもわからず色々調べてみたのですが、これにあったものがなかったため困っています。どこを改善すればよいのでしょうか?
def build_list(): build_list[s] for s in range(0, 501): if range % 2 == 0 or range % 9 == 0: print(range_attend()) for s in range(0, len(build_list)): total = total + build_list[s] print(all) return build_list print('The sum of the list should be 69806.')
またこのコードではエラーも出ずどこを改善すればいいのかがわかりません。
def build_list(): # create an empty list build_list = [] # test all of the numbers through 500 # if they are a multiple of either 2 or 9, then add it onto the end of your list for i in range(501): if i % 2 == 0 or i % 9 == 0: print(build_list, end = ' ') # compute the sum of all of the numbers in the list all = 0 for x in build_list: all = all * x # print the sum of all of the numbers in the list print(all) # return the list (required to pass the tests) print('The sum of the list should be 69806.') build_list()
このような形まで持っていけたのですが、どうすればよいのでしょうか?
The sum of the list should be 69806.
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] 0
Outputはこんな形になりました。
Appendコードを使うことで数字が出てくるようになりましたが、答えがすごく長くなりました。これってくりかえしくり返しなっているということですよね?これを1回限りにするにはどうすればいいのでしょうか?
コードをこのようにしてみました
def build_list(): # create an empty list build_list = [] # test all of the numbers through 500 # if they are a multiple of either 2 or 9, then add it onto the end of your list for i in range(501): if i % 2 == 0 or i % 9 == 0: build_list.append(i) print(build_list) # compute the sum of all of the numbers in the list # print the sum of all of the numbers in the list # return the list (required to pass the tests) print('The sum of the list should be 69806.') build_list()
Outputはこのように500まで数字が出てくるようになったのですが、それがいっぺんに出てこないです。
[0] [0, 2] [0, 2, 4] [0, 2, 4, 6] [0, 2, 4, 6, 8] [0, 2, 4, 6, 8, 9] [0, 2, 4, 6, 8, 9, 10] [0, 2, 4, 6, 8, 9, 10, 12]
print(build_list)をfor文のあとに移動してみると
def build_list(): # create an empty list build_list = [] # test all of the numbers through 500 # if they are a multiple of either 2 or 9, then add it onto the end of your list for i in range(501): if i % 2 == 0 or i % 9 == 0: build_list.append(i) print(build_list) # compute the sum of all of the numbers in the list print(sum(build_list)) # print the sum of all of the numbers in the list # return the list (required to pass the tests) print('The sum of the list should be 69806.') build_list()
こうなりました。しかし同時にエラーコードも出てきました
Traceback (most recent call last): File "python", line 9 print(build_list) ^ SyntaxError: invalid character in identifier
すいません、皆さんの貴重なお時間を使わせていただいて、先程エラーが出たところを再度入力したらエラーが出てこなくなりました。しかしReturnを最後の文に入れたところOutputすら出なくなりました。何が原因なのでしょうか??
def build_list(): # create an empty list build_list = [] # test all of the numbers through 500 # if they are a multiple of either 2 or 9, then add it onto the end of your list for i in range(501): if i % 2 == 0 or i % 9 == 0: build_list.append(i) print(build_list) # compute the sum of all of the numbers in the list print(sum(build_list)) # print the sum of all of the numbers in the list # return the list (required to pass the tests) print('The sum of the list should be 69806.') return build_list()
回答3件
あなたの回答
tips
プレビュー