質問をすることでしか得られない、回答やアドバイスがある。

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

新規登録して質問してみよう
ただいま回答率
85.48%
リストボックス

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

文字コード

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

コードレビュー

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

Python

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

配列

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

Q&A

解決済

3回答

986閲覧

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

このコードは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ページで確認できます。

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

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Spursfun2002

2020/05/09 18:22

すいません、わざわざ教えていただきありがとうございました。
shiracamus

2020/05/09 18:26

関数は呼び出さないと動きませんよ。
Spursfun2002

2020/05/09 18:31

すいません、初心者なので。 どう呼び出せばよいのでしょうか?
shiracamus

2020/05/09 18:35

関数定義が終わったあとに、 build_list() を書いて実行すれば関数を呼び出します。 このプログラムだと関数内でエラーが発生するので、なぜエラーになるのか考えて正しいプログラムに修正していきましょう。
Spursfun2002

2020/05/09 18:45 編集

Traceback (most recent call last): File "python", line 4 build_list[]       ^ SyntaxError: invalid syntax このようなエラーが出た場合何をすればいいのでしょうか?
Spursfun2002

2020/05/09 18:44

Traceback (most recent call last): File "python", line 4 build_list[]  ^ SyntaxError: invalid syntax このような感じです。
shiracamus

2020/05/09 18:46

私、build_list[] って書きましたか? [] ではなく () で呼び出します。
shiracamus

2020/05/09 18:50

print('The sum of the list should be 69806.') で print関数を呼び出しています。関数呼出し引数に文字列を指定しています。
Spursfun2002

2020/05/09 18:50

def build_list(): build_list() for s in range(501): if range % 2 == 0 or range % 9 == 0: print(range_attend()) print(all) print('The sum of the list should be 69806.') build_list() このようにしています。
hoshi-takanori

2020/05/09 18:53 編集

build_list() で呼び出した結果、2 行目の build_list[s] でエラーになってるってことですよね。(同じ名前なので紛らわしい…。) この build_list[s] は、何がどうなることを期待してますか? また、python を勉強するにあたって参考にしている本や資料があったら教えてください。
shiracamus

2020/05/09 18:53 編集

コメントにコードを書くとインデントが正しく表示されないので、正しいのかわかりません。 build_list() は関数定義の後、print('The sum of the list should be 69806.') の次の行にでも書いてみてください。
Spursfun2002

2020/05/09 18:55

このbuild_list[]はempty listを作るためにつけました。 このリストを使用した上で500までの数字をこのリストに入れてその後コードを作っていきたいんですが。 だめでしょうか??
Spursfun2002

2020/05/09 18:57

def build_list(): # create an empty list build_list[] # test all of the numbers through 500 for s in range(501): # if they are a multiple of either 2 or 9, then add it onto the end of your list if range % 2 == 0 or range % 9 == 0: print(range_attend()) # compute the sum of all of the numbers in the list # 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() 説明を入れるとこんな感じになります。どうでしょうか?
Spursfun2002

2020/05/09 18:58

少し修正してみました、ほんとに少しだけですが。
Spursfun2002

2020/05/09 18:59

そういうサイトがあるんですね! すいません初心者すぎて、ありがとうございます。
shiracamus

2020/05/09 20:39

回答欄に書かずに質問欄を更新してください。 リストに追加する処理(append) が見当たりませんよ。 チュートリアルにありませんでしたか?
shiracamus

2020/05/09 21:49

for文の中でprintするのではなく、for文が終わった後にprintを1回だけ実行すればいいです。
Spursfun2002

2020/05/09 21:57

はい。 してみると このようなコードになったんですが、同時にエラーコードも出てきました
shiracamus

2020/05/09 22:08 編集

invalid character in identifier ですから、インデントに使ってる文字が変だというエラーです。 どうやら全角空白を使っているようですね。
Spursfun2002

2020/05/09 22:17

なるほど ということは半角ならいいんですか??
shiracamus

2020/05/10 00:10

自分で試行錯誤しないと成長しませんよ。 これまで何を入力してきたのですか? 同じものを入力してください。
Spursfun2002

2020/05/10 05:46

自分で試行錯誤を繰り返した結果がこの状態です。自分でも不甲斐ない気持ちでいっぱいです。英語と日本語を同じパソコン上で使い分けているので、多分それが入力ミスにつながったようです。
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.48%

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

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

質問する

関連した質問