我试图改变足球队球员的训练数量,并且在尝试使用for循环来实现这一目标时花了太多时间挫败我的脸,这里是与该程序相关的所有代码(你可以跳过它到我将展示我遇到麻烦的部分的部分... ...
import random
import time
from operator import itemgetter
# class that creates each player and manipulates the data associated with the players which is first set here
class NewPlayer:
def __init__(self):
# creates a placeholder name for the player then uses the function (create_name) within to fill with a string
self.name = ""
self.create_name()
# creates the initial rating of a player between and including 10 and 20
self.rating = random.randint(10, 20)
# the amount of times a player has been trained, unique to each instance
self.training_number = 0
def train_player(self):
# if the player gets trained they will get a random rating increase/decrease depending on chance and work level
if -1 < self.training_number < 1:
self.rating += random.randint(1, 5)
# there cant be over excessive training so the more (work) training then the worse the modifier gets as it
# takes a different route based on training_number
self.training_number += 1
print("Training was successful")
# if the player gets retrained they use the following route and end up potentially worse
elif 1 <= self.training_number < 2:
# temporarily store the old value
old = self.rating
self.rating += random.randint(-2, 2)
self.training_number += 1
# the old value is used to compare and give the correct output, relaying the outcome
if self.rating > old:
print("Training was successful")
elif self.rating == old:
print("Nothing new was achieved in training")
else:
print("The training was terrible")
# by this point the player won't receive a positive boost
else:
self.rating += random.randint(-5, -1)
self.training_number += 1
print("The player is too tired and picked up a slight injury")
def create_name(self):
forenames = ["Peter", "Steven", "Michael", "Ishaa", "Nikesh", "George", "Toby", "Jasdeep"]
surnames = ["Chandler", "Evans", "Kelly", "Ward", "Heer", "Trepass", "Gardner", "Walker"]
# randomly creates a name for a player
self.name = random.choice(forenames) + " " + random.choice(surnames)
# class that creates opposition teams that are made up of 11 players
class NewTeam:
def __init__(self):
# variable that holds the team name, currently a placeholder
self.team_name = ""
# variable that holds how many points the team has earned this season, number value += 3 when a match is won,
# 1 when drew and none when lost
self.points = 0
# variable that holds how many upgrade points the opposition earns
self.upgrade_points = 0
# creates the players and the team name for the opposition teams
self.create_team()
self.create_name()
# adds 11 players to any team using the function
def create_team(self):
self.team = []
# adds a goalkeeper to the team
self.team.append(("goalkeeper1:", NewPlayer()))
# adds four defenders to the team
for i in range(1, 5):
self.team.append(("defender" + str(i) + ":", NewPlayer()))
# adds four midfielders to the team
for i in range(1, 5):
self.team.append(("midfielder" + str(i) + ":", NewPlayer()))
# adds two attackers to the team
for i in range(1, 3):
self.team.append(("attacker" + str(i) + ":", NewPlayer()))
# chooses a random name for the team
def create_name(self):
names = ["Birmingham City", "Barnsley", "Accrington Stanley", "Blackburn Rovers", "Blackpool", "AFC Wimbledon",
"Bolton Wanderers", "Bradford City", "Barnet", "Bristol City", "Burton Albion", "Bristol Rovers",
"Brentford", "Bury", "Cambridge United", "Brighton", "Hove Albion", "Chesterfield", "Carlisle United",
"Burnley", "Colchester United", "Crawley Town", "Cardiff City", "Coventry City", "Dagenham",
"Redbridge", "Charlton Athletic", "Crewe Alexandra", "Exeter City", "Derby County", "Doncaster Rovers",
"Hartlepool United", "Fulham", "Fleetwood Town", "Leyton Orient", "Huddersfield Town", "Gillingham",
"Luton Town", "Hull City", "Millwall", "Mansfield Town", "Ipswich Town", "Oldham Athletic",
"Morecambe", "Leeds United", "Peterborough United", "Newport County", "Middlesbrough", "Port Vale",
"Northampton Town", "Milton Keynes", "Notts County", "Nottingham Forest", "Scunthorpe United",
"Oxford United", "Preston North End", "Sheffield United", "Plymouth Argyle", "Queens Park Rangers",
"Shrewsbury Town", "Portsmouth", "Reading", "Southend", "United Stevenage", "Rotherham United",
"Swindon Town", "Wycombe Wanderers", "Sheffield Wednesday", "Walsall", "Yeovil Town",
"Wolverhampton Wanderers", "Wigan Athletic", "York City"]
self.team_name = random.choice(names)
# controls the whole game, incorporates all functions and classes
class Game:
def __init__(self):
rebel = True
# creates the users team
self.my_team = NewTeam()
# allows user customisation of team name
while rebel:
self.my_team.team_name = input("What is your teams name? ")
if len(self.my_team.team_name) >= 50:
rebel = True
print("Too many characters in your team name, please choose one with less than 50 characters - ")
else:
rebel = False
# creating the other teams, using the NewTeam class
self.team1 = NewTeam()
self.team2 = NewTeam()
self.team3 = NewTeam()
self.team4 = NewTeam()
self.team5 = NewTeam()
self.team6 = NewTeam()
self.team7 = NewTeam()
self.team8 = NewTeam()
self.team9 = NewTeam()
# an array to hold all the other teams and keep it neat
self.other_teams = [self.team1, self.team2, self.team3, self.team4, self.team5, self.team6, self.team7,
self.team8, self.team9]
# variable to hold which teams will be facing each other, for later printing in one go
self.matches = []
def match(self):
# creates the five different matches randomly
teams = self.other_teams
match1 = [self.my_team]
match2 = []
match3 = []
match4 = []
match5 = []
random_team = random.choice(teams)
teams.remove(random_team)
match1.append(random_team)
for i in range(2):
random_team = random.choice(teams)
teams.remove(random_team)
match2.append(random_team)
for i in range(2):
random_team = random.choice(teams)
teams.remove(random_team)
match3.append(random_team)
for i in range(2):
random_team = random.choice(teams)
teams.remove(random_team)
match4.append(random_team)
for i in range(2):
random_team = random.choice(teams)
teams.remove(random_team)
match5.append(random_team)
# resets the other teams variable as the teams where removed from before
self.other_teams = [self.team1, self.team2, self.team3, self.team4, self.team5, self.team6, self.team7,
self.team8, self.team9]
# assigns the list of matches
self.matches = [match1, match2, match3, match4, match5]
print("")
print("")
# prints who will be playing who
for match in self.matches:
print(match[0].team_name + " will be playing " + match[1].team_name)
time.sleep(1)
print("")
print("")
def process_turn(self):
# if the player has upgrade points the game asks if they would like to upgrade their players
while True:
player_upgrade = input("Would you like to upgrade your players? Enter yes or no - ")
if player_upgrade == "yes":
self.upgrade_player()
break
if player_upgrade == "no":
print("")
print("")
break
else:
print("That is not a valid input. Try again")
print("")
# the player can either start a new match or view the rankings of the teams
print("Enter \"rankings\" to see the league tables")
print("Enter \"new match\" to play the next match")
while True:
next_input = input("What would you like to do? ")
if next_input == "rankings":
self.rankings()
break
elif next_input == "new match":
self.match()
self.process_matches()
for object in self.my_team.team:
object.training_number = 0
break
else:
print("That is not a valid input... Try again")
def rankings(self):
print("")
print("")
# creates all the teams by adding your team to the other teams
all_teams = [self.my_team] + self.other_teams
# creates ranked_teams where the team name and the teams score are sorted into order
ranked_teams = []
for team in all_teams:
ranked_teams.append((team.points, team.team_name))
# sorts the scores into order
ranked_teams = sorted(ranked_teams, key=itemgetter(0), reverse=True)
# fails to keep that order with secondary key sort
# ranked_teams = sorted(ranked_teams, key=itemgetter(1))
# prints the teams in order of their scores to create the table
counter = 1
for team in ranked_teams:
print(team[1] + " is in " + str(counter) + " place with " + str(team[0]) + " points")
counter += 1
print("")
print("")
def upgrade_player(self):
# prints the players and their score so you can see who to upgrade
for player in self.my_team.team:
print(player[0] + ": " + player[1].name + " has a rating of: " + str(player[1].rating))
print("")
print("")
# loop so that all upgrade points may be used
while True:
# tells the player how many points they have left and asks which player they would like to upgrade
print("Who would you like to upgrade?")
upgrading_input = input("Enter player position to upgrade or type 'exit' to quit upgrading your players - ")
print("")
# each statement checks if a player has been selected for training
# if they have then the score is copied of later use
# then the player is trained
# then the difference in score is printed
# the amount of points available to use is decreased
if upgrading_input == "goalkeeper1":
prev_rating = self.my_team.team[0][1].rating
self.my_team.team[0][1].train_player()
print(self.my_team.team[0][0] + ": " + self.my_team.team[0][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[0][1].rating))
print(self.my_team.team[0][1].name + " has trained " + str(self.my_team.team[0][1].training_number) +
" time(s)")
elif upgrading_input == "defender1":
prev_rating = self.my_team.team[1][1].rating
self.my_team.team[1][1].train_player()
print(self.my_team.team[1][0] + ": " + self.my_team.team[1][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[1][1].rating))
print(self.my_team.team[1][1].name + " has trained " + str(self.my_team.team[1][1].training_number) +
" time(s)")
elif upgrading_input == "defender2":
prev_rating = self.my_team.team[2][1].rating
self.my_team.team[2][1].train_player()
print(self.my_team.team[2][0] + ": " + self.my_team.team[2][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[1][1].rating))
print(self.my_team.team[2][1].name + " has trained " + str(self.my_team.team[0][1].training_number) +
" time(s)")
elif upgrading_input == "defender3":
prev_rating = self.my_team.team[3][1].rating
self.my_team.team[3][1].train_player()
print(self.my_team.team[3][0] + ": " + self.my_team.team[3][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[3][1].rating))
print(self.my_team.team[3][1].name + " has trained " + str(self.my_team.team[3][1].training_number) +
" time(s)")
elif upgrading_input == "defender4":
prev_rating = self.my_team.team[4][1].rating
self.my_team.team[4][1].train_player()
print(self.my_team.team[4][0] + ": " + self.my_team.team[4][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[4][1].rating))
print(self.my_team.team[4][1].name + " has trained " + str(self.my_team.team[4][1].training_number) +
" time(s)")
elif upgrading_input == "midfielder1":
prev_rating = self.my_team.team[5][1].rating
self.my_team.team[5][1].train_player()
print(self.my_team.team[5][0] + ": " + self.my_team.team[5][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[5][1].rating))
print(self.my_team.team[5][1].name + " has trained " + str(self.my_team.team[5][1].training_number) +
" time(s)")
elif upgrading_input == "midfielder2":
prev_rating = self.my_team.team[6][1].rating
self.my_team.team[6][1].train_player()
print(self.my_team.team[6][0] + ": " + self.my_team.team[6][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[6][1].rating))
print(self.my_team.team[6][1].name + " has trained " + str(self.my_team.team[6][1].training_number) +
" time(s)")
elif upgrading_input == "midfielder3":
prev_rating = self.my_team.team[7][1].rating
self.my_team.team[7][1].train_player()
print(self.my_team.team[7][0] + ": " + self.my_team.team[7][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[7][1].rating))
print(self.my_team.team[7][1].name + " has trained " + str(self.my_team.team[7][1].training_number) +
" time(s)")
elif upgrading_input == "midfielder4":
prev_rating = self.my_team.team[8][1].rating
self.my_team.team[8][1].train_player()
print(self.my_team.team[8][0] + ": " + self.my_team.team[8][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[8][1].rating))
print(self.my_team.team[8][1].name + " has trained " + str(self.my_team.team[8][1].training_number) +
" time(s)")
elif upgrading_input == "attacker1":
prev_rating = self.my_team.team[9][1].rating
self.my_team.team[9][1].train_player()
print(self.my_team.team[9][0] + ": " + self.my_team.team[9][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[9][1].rating))
print(self.my_team.team[9][1].name + " has trained " + str(self.my_team.team[9][1].training_number) +
" time(s)")
elif upgrading_input == "attacker2":
prev_rating = self.my_team.team[10][1].rating
self.my_team.team[10][1].train_player()
print(self.my_team.team[10][0] + ": " + self.my_team.team[10][1].name + " had a rating of " +
str(prev_rating) + " and now has rating of " + str(self.my_team.team[10][1].rating))
print(self.my_team.team[10][1].name + " has trained " + str(self.my_team.team[10][1].training_number) +
" time(s)")
# if the input was exit then upgrading of players stops
elif upgrading_input == "exit":
print("quiting")
break
# if the input was incorrect then it asks the input again
else:
print("That was an incorrect input. Try again")
print("")
def process_matches(self):
# for each match the game decides who wins based on player ratings
for match in self.matches:
# obtains the rating of the two teams
rating1 = 0
rating2 = 0
for player in match[0].team:
rating1 += player[1].rating
for player in match[1].team:
rating2 += player[1].rating
rating1 += random.randint(-10, 10)
rating2 += random.randint(-10, 10)
draw = random.randint(1, 20)
# compares the rating of the two teams
if draw == 20:
rating1 = rating2
else:
pass
if rating1 == rating2:
print(match[0].team_name + " has drew against " + match[1].team_name)
match[0].points += 1
match[1].points += 1
match[0].upgrade_points += 1
match[1].upgrade_points += 1
elif rating1 > rating2:
print(match[0].team_name + " has won against " + match[1].team_name)
match[0].points += 3
match[0].upgrade_points += 3
elif rating2 > rating1:
print(match[1].team_name + " has won against " + match[0].team_name)
match[1].points += 3
match[1].upgrade_points += 3
time.sleep(1)
"""need to figure out how the next two lines work, namely, how does the instance have the
training_number attribute yet i can't get hold of and then change it"""
# for player in self.my_team.team:
# player.training_number = 0
# the other teams players gets randomly upgraded if they have enough points
for team in self.other_teams:
for points in range(team.upgrade_points):
player = random.choice(team.team)
player[1].train_player()
team.upgrade_points -= 1
print("")
print("")
game = Game()
while True:
game.process_turn()
# I need to fix the training number, the class has a value of 0, but, the instances/players have their own value
# accrued which will hopefully be reset to 0 after each match played, but hard to do
# need a end game, a simple way of after 10 matches or something it ends season and starts again, your team stays the
# same all others are remade and randomised again
我的问题在于过度训练会产生不利影响的想法,这是一个混乱的代码,可能会更有效,即使有时我感到困惑,我试图让它变得如此匹配(相当于1周)所有玩家的训练数重置为0,以便他们可以再次训练...所以我已经检查过我可以在单个情况下更改值并且能够这样做但是当我尝试在函数process_turn(self)
中播放匹配后不久更改值它似乎不起作用,我尝试在线查看许多示例和其他问题,但最近我似乎找到了是this。然而,我在这里试图解决这部分中包含的for循环...
while True:
next_input = input("What would you like to do? ")
if next_input == "rankings":
self.rankings()
break
elif next_input == "new match":
self.match()
self.process_matches()
**for object in self.my_team.team:
object.training_number = 0**
break
else:
print("That is not a valid input... Try again")
答案 0 :(得分:0)
您的团队属性类型为:[string * NewPlayer]
你正在迭代数组并试图在你的元组上设置training_number属性,这是没有意义的。
您想在元组内的NewPlayer对象上设置training_number属性,如下所示。问题是,元组是不可变的,所以你不能这样做。
for object in self.my_team.team:
object1[1].training_number = 0
相反,您可以将您的团队更改为字典{string:NewPerson}
for role,person in self.my_team.team.iteritems():
person.training_number = 0
然后在您的团队课程中,您必须更改create_team函数以使用字典而不是列表。
self.team = {}
self.team["goalkeeper1"] = NewPlayer()
...