frames = []
frame_total = []
total = 0
while len(frames) < 2:
ball_one = int(raw_input("Enter the score of ball one: "))
ball_two = int(raw_input("Enter the score of ball two: "))
frame_total.append([ball_one, ball_two])
for frame in frame_total:
frames.append(frame_total)
b = sum(frames)
print(frame_total)
print(b)
これは私のコードです。エラーが発生する理由を解決できません TypeError: unsupported operand type(s) for +: "int" and "list"
メイン配列内の各配列の合計を印刷しようとすると。何か案は?
アイデアは、ボウリングゲームを作成することです。 すべてのフレームに、ball_oneとball_twoの2つのボールがあります。各フレームの後、これら2つの数値を配列に追加します。したがって、ball_oneが8でball_twoが1の場合、[[8、2]]が追加され、次にb_1が4でb_2が4の場合は[[8、2]、[4、4]のようになります]。次に、8と2と4と4を追加して作成します。 [[10、8]]。そして最終的にこれら両方の合計、18。
回答:
回答№1は0私が試してみましょう。 各ペア[ball_one、ball_two]をフレームに追加します。その後、ペアごとの合計を取得し、別のリストに保存して、frames_totalと言い、最後にすべての合計を表示します。コードは次のとおりです。
frames = [] # will contain raw results in each frame. (ex. [[8, 2], [4, 4]])
frames_total = [] # will contain total values in each frame (ex. [10, 8])
total = 0 # sum of everything
while len(frames) < 2:
ball_one = int(raw_input("Enter the score of ball one: "))
ball_two = int(raw_input("Enter the score of ball two: "))
frames.append([ball_one, ball_two])
frames_total = [sum(frame) for frame in frames]
total = sum(frames_total)
print frames_total
print total
毎ターン後に情報を印刷したい場合は、ここに少し異なるコードがあります:
frames = [] # will contain raw results in each frame. (ex. [[8, 2], [4, 4]])
frames_total = [] # will contain total values in each frame (ex. [10, 8])
total = 0 # sum of everything
while len(frames) < 2:
ball_one = int(raw_input("Enter the score of ball one: "))
ball_two = int(raw_input("Enter the score of ball two: "))
frames.append([ball_one, ball_two])
frames_total.append(ball_one + ball_two)
total = sum(frames_total)
print frames
print frames_total
print total
回答№2の場合は1
定義し続けます
b
それぞれの反復でfor
ループ。実際に必要になるまで、定義を待つ必要があります。追加し続けます
frame_total
にframes
の代わりにframe
.各リスト内のすべての数字ではなく、リストのリストの合計を見つけようとします。
更新されたプログラム:
frame_total = []
while len(frames) < 2:
ball_one = int(raw_input("Enter the score of ball one: "))
ball_two = int(raw_input("Enter the score of ball two: "))
frame_total.append([ball_one, ball_two])
print(frame_total)
print(sum(sum(sub) for sub in frame_total))
ジェネレータ式が気に入らない場合:
frame_total = []
total = 0
while len(frames) < 2:
ball_one = int(raw_input("Enter the score of ball one: "))
ball_two = int(raw_input("Enter the score of ball two: "))
frame_total.append([ball_one, ball_two])
total += ball_one + ball_two
print(frame_total)
print(total)
回答№3の場合は0
sum(iterable [、start])
合計が始まり、イテレート可能な項目から 左から右に合計を返します。 startのデフォルトは0です。 iterableのアイテムは通常数字であり、開始値は 文字列にすることができます。
リストのリストでsumを呼び出しています。そしてあなたが供給していないので start
、デフォルトではゼロです。したがって、基本的にintとリストを追加しようとします。
試してみてください 0 + [1,2,3]
そして、あなたはあなたのエラーが何であるかを理解します。
回答№4の場合は0
きみの frame_total
1つの要素があり、それは [ball_one, ball_two]
。追加するとき frame_total
に frames
, frames
現在1つの要素があり、それはまだです [ball_one, ball_two]
。合計すると、フレームが発生します TypeError
なぜなら frames
要素は list
ない int
.
回答№5の場合は0
次のように変更します。
frames = []
frame_total = []
total = 0
while len(frames) < 2:
ball_one = int(input("Enter the score of ball one: "))
ball_two = int(input("Enter the score of ball two: "))
frame_total.append(ball_one)
frame_total.append(ball_two)
b = 0
for frame in frame_total:
frames.append(frame_total)
b = b + frame
print(frame_total)
print(b)