python おさらい その2
最初に入力回数をいれて
次に文字列をいれ、それを出力していく
1 2 3 4 | input_line = int ( input ()) for i in range (input_line): string = input () print (string) |
標準入力で空白区切り、もしくはスペース区切りの場合は
数値の場合なら
Python3 標準入力から複数の値を受け取りたい時
のように
1 | map ( int , input (),split()) |
を使う
文字列の場合は
1 | input ().split() |
でOK
スペース区切りの文字列を分割して
2行にするのなら
1 2 3 4 | std_in = input () for string in std_in.split(): print (string) |
というかんじ
次にスペース区切りで2つの整数を入力し
それを足すというもの
1 2 | a,b = map ( int , input ().split()) print (a + b) |
でOK
2つの整数をそれぞれ足して
最後に合計を出す
もし同じ2つの値のときには掛け算する
1 2 3 4 5 6 7 8 9 10 11 12 13 | time = int ( input ()) result = 0 for i in range (time): std_in = input () array = std_in.split() if array[ 0 ] = = array[ 1 ]: result + = int (array[ 0 ]) * int (array[ 1 ]) else : result + = int (array[ 0 ]) + int (array[ 1 ]) print (result) |