質問するログイン新規登録

Q&A

解決済

3回答

1477閲覧

0〜500までの数字で2と9の倍数である数字をリスト化しそれらの数字をすべて足したい。

Spursfun2002

総合スコア15

リストボックス

ユーザーがリストから1つ以上のアイテムを選択できるようにするGUI要素です。

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

1グッド

1クリップ

投稿2020/05/09 18:02

編集2020/05/09 22:12

1

1

このコードは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()
DrqYuto👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

guest

回答3

0

ベストアンサー

お手数をかけてしまいほんとに申し訳ないです。
自分で解決できたのでコードを貼っておきます。

def build_list(): build_list = [] for i in range(501): if i % 2 == 0 or i % 9 == 0: build_list.append(i) return build_list build_list() print('The sum of the list should be 69806.')

投稿2020/05/10 12:08

Spursfun2002

総合スコア15

Spursfun2002

2020/05/10 12:08

協力してくださった方々、ほんとにありがとうございました。
guest

0

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()

このような形まで持っていけたのですが、このままだとOutputが
The sum of the list should be 69806.
[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] 0

このようになってしまいます。どうしてでしょう??

投稿2020/05/09 19:53

Spursfun2002

総合スコア15

Spursfun2002

2020/05/09 21:13

なるほど わざわざ見つけていただきありがとうございます。
guest

0

以下でどうでしょう?

print(sum([i for i in range(501) if i % 18 == 0]))

出力
6804

print(sum([i for i in range(501) if i % 2 == 0 or i % 9 == 0])) ``` 69806 ということは、こういうことでしょうか? ``` def build_list(): print(sum([i for i in range(501) if i % 2 == 0 or i % 9 == 0])) build_list() ```

投稿2020/05/09 22:08

編集2020/05/10 01:37
DrqYuto

総合スコア432

Spursfun2002

2020/05/09 22:12

修正しましたが、なにもでませんでした。 回答ありがとうございます。
DrqYuto

2020/05/09 22:14

それは良かったです。
DrqYuto

2020/05/09 22:48

orで、書き直しました。お試しください。
Spursfun2002

2020/05/09 23:32

すいませんわざわざ修正してくれてありがとうございます。 しかし何も出なくなってしまいました。ほんとに申し訳ないです。
Spursfun2002

2020/05/09 23:33

print(build_list)でもリストは出たので他のところがだめなんですかね?
Zuishin

2020/05/09 23:37

「他のところ」があるならダメでしょうね。これ一行で完結してます。
Spursfun2002

2020/05/10 05:47

なるほど、他のところを使いながら作りたいんです。 ほんとに申し訳ないです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.25%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問