我需要完成一项任务,我需要获取两支球队得分的次数,并将其放入下表中的表格中。我确实创建了一个空数组来保存用户的输入,然后将其附加到该数组。我希望我做对了。我在编写代码来确定获胜者时遇到了麻烦(获胜者专栏)。我知道我需要执行IF / ELSE语句,但不确定将代码放在哪里。
所需的输出:
Team 1 Team 2 Winner
Match 1 25 22 Team 1
Match 2 25 41 Team 2
Match 3 30 40 Team 2
Winner is Team 2
我尝试将if / else语句放入每个“ For Loop”中,但没有得到想要的结果。有没有更好的方法来编码我已经完成的工作?
我的代码:
team1_scores = []
team2_scores = []
matches = ['Match 1', 'Match 2', 'Match 3', 'Match 4', 'Match 5']
for i in range(5):
team1_scores_input = int(input("Enter scores for Team 1: "))
team1_scores.append(team1_scores_input)
for i in range(5):
team2_scores_input = int(input("Enter scores for Team 2: "))
team2_scores.append(team2_scores_input)
print("{:>20s} {:>13s} {:>15s}".format("Team 1 ", "Team 2", "Winner"))
print("{} {:6d} {:>14d}".format(matches[0], team1_scores[0],
team2_scores[0]))
print("{} {:6d} {:>14d}".format(matches[1], team1_scores[1],
team2_scores[1]))
print("{} {:6d} {:>14d}".format(matches[2], team1_scores[2],
team2_scores[2]))
print("{} {:6d} {:>14d}".format(matches[3], team1_scores[3],
team2_scores[3]))
print("{} {:6d} {:>14d}".format(matches[4], team1_scores[4],
team2_scores[4]))
当前输出
Enter scores for Team 1: 20
Enter scores for Team 1: 25
Enter scores for Team 1: 20
Enter scores for Team 1: 26
Enter scores for Team 1: 50
Enter scores for Team 2: 40
Enter scores for Team 2: 60
Enter scores for Team 2: 20
Enter scores for Team 2: 10
Enter scores for Team 2: 20
Team 1 Team 2 Winner
Match 1 20 40
Match 2 25 60
Match 3 20 20
Match 4 26 10
Match 5 50 20
答案 0 :(得分:0)
from collections import Counter
team1_scores = []
team2_scores = []
winning_res = []
matches = ['Match 1', 'Match 2', 'Match 3', 'Match 4', 'Match 5']
for i in range(5):
team1_scores_input = int(input("Enter scores for Team 1: "))
team1_scores.append(team1_scores_input)
for i in range(5):
team2_scores_input = int(input("Enter scores for Team 2: "))
team2_scores.append(team2_scores_input)
print("{:>20s} {:>13s} {:>15s}".format("Team 1 ", "Team 2", "Winner"))
for i in range(5):
win = ""
max_score = max(team1_scores[i], team2_scores[i])
if max_score == team1_scores[i] and max_score == team2_scores[i]:
win = "Tie"
elif max_score == team1_scores[i]:
win = "Team 1"
elif max_score == team2_scores[i]:
win = "Team 2"
print("{} {:6d} {:>14d} {:>22s}".format(matches[i], team1_scores[i], team2_scores[i], win))
overall_winner_dict = Counter(winning_res)
max_value = None
for key in overall_winner_dict:
if max_value is None or max_value < overall_winner_dict[key]:
max_value = overall_winner_dict[key]
max_key = key
输出:
Enter scores for Team 1: 43
Enter scores for Team 1: 54
Enter scores for Team 1: 65
Enter scores for Team 1: 34
Enter scores for Team 1: 54
Enter scores for Team 2: 65
Enter scores for Team 2: 34
Enter scores for Team 2: 65
Enter scores for Team 2: 23
Enter scores for Team 2: 45
Team 1 Team 2 Winner
Match 1 43 65 Team 2
Match 2 54 34 Team 1
Match 3 65 65 Tie
Match 4 34 23 Team 1
Match 5 54 45 Team 1
Overall Winner: Team 1
答案 1 :(得分:0)
还有许多更干净,更精确的方法可以执行此操作,但是现在,您可以使用此方法进行管理。
team1_scores = []
team2_scores = []
matches = ['Match 1', 'Match 2', 'Match 3', 'Match 4', 'Match 5']
winner=[]
for i in range(5):
team1_scores_input = int(input("Enter scores for Team 1: "))
team1_scores.append(team1_scores_input)
for i in range(5):
team2_scores_input = int(input("Enter scores for Team 2: "))
team2_scores.append(team2_scores_input)
for i in range(5):
if team1_scores[i]>team2_scores[i]:
winner.append("Team 1")
else:
winner.append("Team 2")
final_winner = "Team 1" if winner.count("Team 1")>winner.count("Team 2") else "Team 2"
print("{:>20s} {:>13s} {:>15s}".format("Team 1 ", "Team 2", "Winner"))
print("{} {:6d} {:>14d} {:>20s}".format(matches[0], team1_scores[0],
team2_scores[0],winner[0]))
print("{} {:6d} {:>14d} {:>20s}".format(matches[1], team1_scores[1],
team2_scores[1],winner[1]))
print("{} {:6d} {:>14d} {:>20s}".format(matches[2], team1_scores[2],
team2_scores[2],winner[2]))
print("{} {:6d} {:>14d} {:>20s}".format(matches[3], team1_scores[3],
team2_scores[3],winner[3]))
print("{} {:6d} {:>14d} {:>20s}".format(matches[4], team1_scores[4],
team2_scores[4],winner[4]))
print("Winner is",final_winner)
答案 2 :(得分:0)
您可以在填充两个列表之后添加简单的条件检查。除了违反DRY原理外,您还可以将要显示的所有内容放入一个循环中。您可以像其他答案中提到的那样创建一个winners
列表,但是使用此方法,您不必这样做(不是,这是更受欢迎的方法):
# ... after asking user input...
for i in range(len(matches)):
print("{} {:>6d} {:>14d} {:>19s}".format(matches[i], team1_scores[i],
team2_scores[i], \
"Team1" if team1_scores[i] > team2_scores[i] else "" \
"Team2" if team1_scores[i] < team2_scores[i] else "Draw"))
注意:\
仅用于提高python代码的可读性。单线会不必要地长
答案 3 :(得分:0)
在下面添加了评论示例。我将其配置为可配置的,因此您可以根据需要添加任意多个匹配项。另外,我认为同时输入两支球队每场比赛的统计数据以减少重复更为直观。
WINNER_FLAGS = ['Tie', 'Team 1', 'Team 2']
matches = ['Match 1', 'Match 2', 'Match 3'] # configurable, can add more matches
def determine_winner(score1, score2):
''' given two scores, output corresponding winner or tie '''
if score1 == score2:
return WINNER_FLAGS[0]
elif score1 > score2:
return WINNER_FLAGS[1]
else:
return WINNER_FLAGS[2]
match_stats = []
win_counter = {flag: 0 for flag in WINNER_FLAGS} # track all winners
for match_name in matches:
# enter scores for each team
print '\nDetermine {} stats: \n'.format(match_name)
team1_score = int(input("Enter score for Team 1: "))
team2_score = int(input("Enter score for Team 2: "))
# track the winner
winner = determine_winner(team1_score, team2_score)
win_counter[winner] += 1
# store stats for displaying later
match_stats.append((match_name, team1_score, team2_score, winner))
# display stats
print("{:>20s} {:>13s} {:>15s}".format("Team 1 ", "Team 2", "Winner"))
for match in match_stats:
match_name, team1_score, team2_score, winner = match
print("{} {:6d} {:>14d} {:>20s}".format(match_name, team1_score, team2_score, winner))
# extract the top winner !
max_wins = 0
ultimate_winner = None
for winner, num_wins in win_counter.items():
if num_wins > max_wins:
ultimate_winner = winner
print("Ultimate winner is {}".format(ultimate_winner) if ultimate_winner != WINNER_FLAGS[0] else 'Its a tie!!!!')
样品运行:
Determine Match 1 stats:
Enter score for Team 1: 10
Enter score for Team 2: 10
Determine Match 2 stats:
Enter score for Team 1: 2
Enter score for Team 2: 39
Determine Match 3 stats:
Enter score for Team 1: 1
Enter score for Team 2: 90
Team 1 Team 2 Winner
Match 1 10 10 Tie
Match 2 2 39 Team 2
Match 3 1 90 Team 2
Ultimate winner is Team 2