【发布时间】:2020-03-04 09:32:17
【问题描述】:
我正在尝试使用 Python 编写一个程序,该程序会询问用户 5 场比赛的分数。然后我需要在最后打印每场比赛的获胜者。我遇到的问题是为每场比赛找到合适的获胜者进行打印。我只能使用列表、for 循环和 if/else 语句。我是编程新手,所以我不确定我做错了什么。
说明:程序将接受每支球队在 5 场比赛中得分的数量,并将数据与获胜者一起打印出来。获胜的球队是赢得比赛最多的球队。注意:不一定是得分最多的球队。
games = [0, 0, 0, 0, 0]
winner = ["Team 1", "Team 2"]
gamesresults = 0
team1_scores = [0, 0, 0, 0, 0]
team2_scores = [0, 0, 0, 0, 0]
matches = ["Match 1", "Match 2", "Match 3", "Match 4", "Match 5"]
for games in range(5):
team1_scores[games] = int(input("Enter the score from team 1 from match {}?".format(games+1)))
team2_scores[games] = int(input("Enter the score from team 2 from match {}?".format(games+1)))
games += 1
#the section i'm having problems with
for games in range(5):
if team1_scores[games] > team2_scores[games]:
winner = "Team 1"
else:
winner = "Team 2"
gamesresults = [team1_scores, team2_scores, winner]
#this is ok
print(" ", "Team 1", "Team 2", "Winner")
for i in range(5):
print("Matches", i+1, team1_scores[i], team2_scores[i], winner)
if team1_scores > team2_scores:
print("The Winner is Team 1")
else:
team2_scores > team1_scores
print("The Winner is Team 2")
【问题讨论】:
-
游戏规则是什么,比如获胜者是如何决定的?是总分最高还是别的什么?
-
程序将接受每支球队在 5 场比赛中的得分,并将数据与获胜者一起打印出来。获胜的球队是赢得比赛最多的球队。注意:不一定是得分最多的球队。
-
我遇到的问题是让每场比赛的正确获胜者打印出来。你能说得更具体点吗?
标签: python list for-loop if-statement