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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Python

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

Q&A

解決済

2回答

2502閲覧

アウトプット方法がわからない

udairhythm

総合スコア17

Python

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

0グッド

0クリップ

投稿2018/10/06 09:50

□アウトプット
team win lose draw rate
のように表示させたいですが、記載方法がわかりません。どのようにおこなえばよろしいでしょうか。

print(f"{team:8s} {win:3d} {lose:3d} {draw:3d} {rate:3f}")

ーーーーーーーーーーーーーーーーーーーーーーーー
from baseball_team import Baseball_Team

list = []

Carp = Baseball_Team("Carp")
Tigers = Baseball_Team("Tigers")
BayStars = Baseball_Team("BayStars")
Giants = Baseball_Team("Giants")
Dragons = Baseball_Team("Dragons")
Swallows = Baseball_Team("Swallows")

Carp.name = "Carp"
Carp.win = 88
Carp.lose = 51
Carp.draw = 4

Tigers.name = "Tigers"
Tigers.win = 78
Tigers.lose = 61
Tigers.draw = 4

BayStars.name = "BayStars"
BayStars.win = 73
BayStars.lose = 65
BayStars.draw = 5

Giants.name = "Giants"
Giants.win = 72
Giants.lose = 68
Giants.draw = 3

Dragons.name = "Dragons"
Dragons.win = 59
Dragons.lose = 79
Dragons.draw = 5

Swallows.name = "Swallows"
Swallows.win = 45
Swallows.lose = 96
Swallows.draw = 2

Carp.calc_win_rate()
Tigers.calc_win_rate()
BayStars.calc_win_rate()
Giants.calc_win_rate()
Dragons.calc_win_rate()
Swallows.calc_win_rate()

print(f"{team:8s} {win:3d} {lose:3d} {draw:3d} {rate:3f}")
print(f"{Carp.name:8s} {Carp.win:3d} {Carp.lose:3d} {Carp.draw:3d} {Carp.rate:.3f}")
print(f"{Tigers.name:8s} {Tigers.win:3d} {Tigers.lose:3d} {Tigers.draw:3d} {Tigers.rate:.3f}")
print(f"{BayStars.name:8s} {BayStars.win:3d} {BayStars.lose:3d} {BayStars.draw:3d} {BayStars.rate:.3f}")
print(f"{Giants.name:8s} {Giants.win:3d} {Giants.lose:3d} {Giants.draw:3d} {Giants.rate:.3f}")
print(f"{Dragons.name:8s} {Dragons.win:3d} {Dragons.lose:3d} {Dragons.draw:3d} {Dragons.rate:.3f}")
print(f"{Swallows.name:8s} {Swallows.win:3d} {Swallows.lose:3d} {Swallows.draw:3d} {Swallows.rate:.3f}")
ーーーーーーーーーーーーーーーーー

ec2-user:~/environment $ python exec_baseball_team.py
team win lose draw rate
Carp 88 51 4 0.633
Tigers 78 61 4 0.561
BayStars 73 65 5 0.529
Giants 72 68 3 0.514
Dragons 59 79 5 0.428
Swallows 45 96 2 0.319

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

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

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

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

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

LouiS0616

2018/10/06 09:54

希望通りに出力できているように見えますが、何が疑問なのでしょうか?
udairhythm

2018/10/06 09:56

失礼しました、実際にはこのようなエラーがでます ec2-user:~/environment $ python use_baseball_team.py Traceback (most recent call last): File "use_baseball_team.py", line 49, in <module> print(f"{team:8s} {win:3d} {lose:3d} {draw:3d} {rate:3f}") NameError: name 'team' is not defined
guest

回答2

0

このようなエラーがでます

Traceback (most recent call last): File "use_baseball_team.py", line 49, in <module> print(f"{team:8s} {win:3d} {lose:3d} {draw:3d} {rate:3f}") NameError: name 'team' is not defined

フォーマット済み文字列リテラルは、変数の値を簡単に埋め込める機能です。

Python

1>>> answer = 42 2>>> 3>>> print('answer') 4answer 5>>> print(f'answer') 6answer 7>>> print(f'{answer}') 842 9>>> print(f'{answer:06d}') 10000042

文字列をそのまま出力したいときに利用するのには違和感があります。
普通にprintすれば良いです。


何かしらのサンプルコードを参考にしたのだと推察します。
しかし単にコピペするのではなく、ある程度理解してから利用するようにしてください。

書式の指定方法を少し齧っていれば、次の記述が何重にもおかしいことがわかるはずです。

Python

1f'{draw:3d}'
  1. drawという変数を用意していない
  2. 文字列を表示したいのだとすれば、整数形式の出力dを指定するのはおかしい
  3. そもそも3文字幅にどうやって4文字の文字列を出力するのか

表形式のデータを処理するのは案外面倒です。
今後の拡張のためにも、pandasを利用することをお勧めします。

コードの書き方について

teratailには、コードを見やすく表示する機能があります。
質問編集画面を開き、コードを選択した状態で<code>ボタンを押してください。
Python
特にPythonの場合、インデントが崩れるとコードの意味が変わってしまいます。

投稿2018/10/06 10:10

編集2018/10/06 10:21
LouiS0616

総合スコア35658

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

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

0

ベストアンサー

f-format を利用したサンプルコードを貼っておきます。

使い方に関する 参考リンク

サンプルコード

python

1class Baseball_Team: 2 def __init__(self, name): 3 self.name = name 4 self.win = 0 5 self.lose = 0 6 self.draw = 0 7 8 def get_winrate(self): 9 return self.win / (self.lose + self.lose + self.draw) 10 11Carp = Baseball_Team("Carp") 12Tigers = Baseball_Team("Tigers") 13BayStars = Baseball_Team("BayStars") 14Giants = Baseball_Team("Giants") 15Dragons = Baseball_Team("Dragons") 16Swallows = Baseball_Team("Swallows") 17 18Carp.win = 88 19Carp.lose = 51 20Carp.draw = 4 21 22Tigers.win = 78 23Tigers.lose = 61 24Tigers.draw = 4 25 26BayStars.win = 73 27BayStars.lose = 65 28BayStars.draw = 5 29 30Giants.win = 72 31Giants.lose = 68 32Giants.draw = 3 33 34Dragons.win = 59 35Dragons.lose = 79 36Dragons.draw = 5 37 38Swallows.win = 45 39Swallows.lose = 96 40Swallows.draw = 2 41 42teams = [Carp, Tigers, BayStars, Giants, Dragons, Swallows] 43 44# header 45print('{:10s} {:3s} {:4s} {:4s} {:6s}'.format( 46 'team', 'win', 'lose', 'draw', 'rate')) 47for team in teams: 48 winrate = team.get_winrate() 49 print(f'{team.name:<10s} {team.win:<3d} {team.lose:<4d} {team.draw:<4d} {winrate:<6.2%}') 50# Python 3.6 以前なら format() を使う。 51# print('{0.name:10s} {0.win:3d} {0.lose:3d} {0.draw:3d} {1:5.2%}'.format( 52# team, team.get_winrate()))

出力

team win lose draw rate Carp 88 51 4 83.02% Tigers 78 61 4 61.90% BayStars 73 65 5 54.07% Giants 72 68 3 51.80% Dragons 59 79 5 36.20% Swallows 45 96 2 23.20%

投稿2018/10/06 10:23

編集2018/10/06 15:26
tiitoi

総合スコア21954

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

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

udairhythm

2018/10/06 15:19

ありがとうございます、この出力の前に、 team win lose draw rate を入れたいのですが、どのようにおこなえばよろしいでしょうか。 こうしたい team win lose draw rate Carp 88 51 4 0.633 Tigers 78 61 4 0.561 BayStars 73 65 5 0.529 Giants 72 68 3 0.514 Dragons 59 79 5 0.428 Swallows 45 96 2 0.319
tiitoi

2018/10/06 15:27

ヘッダーを print して、各フィールド幅が合うように回答を修正しました。
udairhythm

2018/10/06 16:08

ありがとうございます!formatの使い方がわかりました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問