Search code examples
pythonfunctiontransfer

How to transfer values from one function to the rest of the project


I'm coding a soccer game and coins is set to 2000 at the start but whenever I buy a player it doesn't adjust the coins (replit is the platform it's being coded on so replit.clear() clears the whole screen).

Transfer market function:

def transfer_market (coins, gk, rb, rcb, lcb, lb, rcm, cm, lcm, rw, st, lw):
  #Gets your goalie

  #Chooses goalkeeper number
  goalkeeper_number = random.randrange (0, 24+1)
  #Chooses goalkeeper
  goalkeeper = goalkeepers [goalkeeper_number]
  #Pulls goalkeeper rating
  global goalkeeper_rating
  goalkeeper_rating = goalkeeper_ratings [goalkeeper_number]
  #Generates goalkeeper price
  goalkeeper_price = 15 * (goalkeeper_rating * 0.5)

  replit.clear()
  print (("""Welcome to the transfer market. You have %d coins. Your avaliable players are:
  """)%(coins))

  #Lists stuff for goalkeeper
  want_goalkeeper = str(input((("""Position: Goalkeeper
  Name: %s
  Rating: %d
  Price: %d

  Do you want him (press 1 to sign): """)%(goalkeeper, goalkeeper_rating, goalkeeper_price))))

  if want_goalkeeper == '1' and coins < goalkeeper_price:
    print ("Sorry. You can't get him.")
  else:
    coins = coins - goalkeeper_price
    print (("You have just signed %s. You have %d coins remaining.")%(goalkeeper, coins))

Whole code:

import random
import replit
import time

#Sets how many coins you have at the start 
coins = 2000
newcoins = 0
goalkeeper_rating = 0

#Lists all the possible teams
teams = ["Manchester_United", "Independente", "QPR", "Everton", "Atlanta_United", "Sheffield_United", "Crystal_Palace", "Juventus", "Chelsea", "Bayern", "Arsenal", "Brentford", "Liverpool", "Manchester_City", "Portland_Timbers", "Derby_County"]

#Lists all the goalkeepers (25 in total)
goalkeepers = ["De_Gea", "Romero", "Campana", "Lumley","Pickford", "Guzan", "Henderson", "Verrips", "Guaita","Szczęsny", "Buffon", "Kepa", "Caballero", "Neuer", "Ulriech", "Früchtl", "Leno", "Martínez", "David Raya", "Daniels", "Alisson", "Ederson", "Attinella", "Roos", "Hamer"]

#Lists the goalkeepers ratings in order (EG De_gea = 89)
goalkeeper_ratings = [89, 80, 79, 66, 83, 75, 76, 67, 82, 86, 84, 85, 75, 89, 80, 65, 86, 74, 72, 65, 88, 87, 70, 70, 62]

#Lists all the left backs
left_backs = ["Shaw", "Miño", "Manning", "Dinge", "Shea", "Bello", "Stevens", "Osborn", "Aanholt", "Sandro", "Alaba", "Tierney", "Henry", "Robertson", "Zinchenko", "Mendy", "Villafaña", "Malone", "Buchanan"]

#Lists the left backs ratings (EG Shaw 81)
leftback_ratings = [81, 74, 68, 83, 64, 59, 76, 73, 77, 86, 85, 79, 70, 85, 75, 80, 70, 71, 55]

#Lists all the center backs
center_backs = ["Maguire", "Lindelof", "Tuanzebe", "Silva","Franco", "Figal", "Barboza", "Barbet", "Hall", "Mina", "Keane", "Pirez", "Robinson", "Escobar", "Pogba", "Parkhurst", "O'Connell", "Egan", "Basham", "Jagielka", "Stearman", "Sahko", "Tomkins", "Chiellini", "De Ligt", "Bonucci", "Demiral", "Rugani", "Rudiger", "Christensen", "Hernandez", "Süle", "Boateng", "Pavard", "Mai", "David Luiz", "Sokratis", "Chambers", "Mustafi", "Jeanvier", "Pinnock", "Jansson", "Sorenson", "Van Dijk", "Matip", "Laporte", "Stones", "Mabiala", "Valentin", "Keogh", "Clarke", "Davies"]

#Lists the center backs ratings (EG Maguire 83)
centerback_ratings = [83, 82, 76, 74, 77, 75, 73, 70, 68, 79, 82, 75, 68, 68, 70, 68, 77, 75, 74, 76, 69, 81, 78, 90,84, 85, 78, 81, 84, 81, 83, 85, 84, 79, 65, 82, 84, 79, 77,72, 69, 78, 65, 91, 83, 87, 83, 72, 71, 75, 71, 72]

#Lists all the right backs
right_backs = ["Wan-Bissaka", "Dalot", "Young", "Bustos", "Rangel", "Coleman", "Baldock", "Freeman", "Ward", "Danilo", "Sciglio", "Kimmich", "Bellerin", "Maitland-Niles","Dalsgaard", "Alexander-Arnold", "Cancelo", "Bogle"]

#Lists the right backs ratings (EG Wan-Bissaka 81)
rightback_ratings = [81, 75, 73, 77, 70, 80, 73, 66, 73, 80, 77, 87, 80, 75, 68, 82, 85, 70]

#Lists all the center mids (CDMS and CAMS are listed as just CM's)
center_mids = ["Pogba", "McTominay", "Pereira", "Matic", "Fred", "Gomes", "Romero", "Domingo", "Perez", "Benítez", "Hernández", "Roa", "Scowen", "Cameron", "Amos", "Sigurdson", "Gomes", "Gbamin", "Barco", "Remedi", "Nagbe","Hyndman", "Larentowicz", "Adams", "Fleck", "Norwood", "Freeman", "Lundstram", "Morrison", "Bešić", "Meyer", "McArthur", "Milivojevič", "Can", "Rabiot", "Ramsey", "Bentancur", "Pjanic", "Khedira", "Thiago", "Goretzka", "Tolisso", "Martínez", "Sanches", "Cuisance", "Torreira", "Ceballos", "Guendouzi", "Xhaka", "Ozil", "Jensen", "Mokotjo", "Nørgaard", "Dasilva", "Wijnaldum", "Henderson", "Fabinho", "De Bruyne", "Fernandinho", "Rodri", "Paredes", "Valeri", "Chará", "Dowell", "Huddlestone", "Bielek", "Evans", "Knighton"]

#Lists the center mids ratings (EG Pogba 88)
centermid_ratings = [88, 76, 78, 82, 80, 68, 74, 77, 76, 75, 76, 73, 69, 73, 68, 83, 82, 79, 74, 71, 75, 67, 70, 66,75, 77, 76, 69, 74, 73, 79, 77, 81, 82, 83, 83, 79, 85, 83,89, 84, 82, 83, 75, 75, 83, 83, 77, 81, 84, 74, 73, 72, 66,83, 83, 86, 91, 88, 83, 70, 79, 75, 73, 72, 69, 68, 64]

#Lists all of the left wingers
left_wingers = ["Martial", "Alexis Sanchez", "Chong", "Domínguez", "Pizzini", "Eze", "Bernard", "Meram", "Carleton", "Pereira", "Zaha", "Pulisic", "Ronaldo", "Coutinho", "Perišić", "Coman", "Davies", "Aubameyang", "Canós", "Mbeumo", "Sterling", "Blanco", "Lawrence"]

#Lists all of the left wingers ratings (EG Martial 84)
leftwing_ratings = [84, 83, 66, 75, 72, 70, 80, 72, 62, 64,83, 80, 93, 87, 85, 83, 74, 89, 70, 68, 89, 78, 75]

#Lists all of the strikers
strikers = ["Rashford", "Greenwood", "Romero", "Palacios", "Hugill", "Calvert-Lewin", "Martínez", "Martínez", "Vazquez", "Robinson", "McBurnie", "Sharp", "McGoldrick","Mousset", "Benteke", "Dybala", "Higuan", "Mandžukić", "Lewandowski", "Müller", "Lacazette", "Nketiah", "Watkins", "Marcondes", "Aguero", "Brian Fernández", "Waghorn", "Marriott"]

#Lists all of the strikers ratings (EG Rashford 82)
striker_ratings = [82, 67, 74, 73, 72, 76, 82, 78, 63, 73, 76, 76, 74, 70, 77, 88, 86, 82, 90, 85, 86, 71, 75, 64, 90,77, 74, 76]

#Lists all of the right wingers
right_wingers = ["Lingard", "Mata", "James", "Oyasi-Samuel", "Richarlison", "Gressel", "Villalba", "Townsend", "Douglas Costa", "Cuadrado", "Bernardeschi", "Gnabry", "Pepe", "Mkhitaryan", "Nelson", "Benrahma", "Valencia", "Bernando Silva", "Polo", "Bennett"]

#Lists all of the right wingers ratings (EG Lingard 80)
rightwing_ratings = [80, 82, 72, 64, 81, 74, 73, 80, 86, 83, 82, 84, 82, 80, 75, 73, 65, 88, 69, 70]


#This function actually simulates the matches
def sim_game(hname, aname, off, deff, opp_off, opp_deff):
  attempts_allowed = (round((off + deff)/20))

  x = round (.5 * (off/(off+opp_deff)), 4)
  x *= 10000

  goals = 0

  for i in range (attempts_allowed + 1):
    number = random.randrange(1, 10000+1)

    if number >= 1 and number < x:
      goals += 1

  print (("> %s scored %d goals.")%(hname, goals))

  attempts_allowed = (round((opp_off + opp_deff)/20))

  x = round (.5 * (opp_off/(opp_off+deff)), 4)
  x *= 10000

  away_goals = 0

  for i in range (attempts_allowed + 1):
    number = random.randrange(1, 10000+1)

    if number >= 1 and number < x:
      away_goals += 1

  print (("> %s scored %d goals.")%(aname, away_goals))

#This function loads out your lineup
def print_lineup (club_name, gk, rb, rcb, lcb, lb, rcm, cm, lcm, rw, st, lw):
  replit.clear()
  print(("""
%s's lineup:

%s         %s         %s
LW         ST         RW

 %s        %s        %s
 CM        CM        CM

%s     %s      %s      %s
LB     CB      CB      RB
           %s
           GK""")%(club_name, lw, st, rw, rcm, cm, lcm, lb,lcb, rcb, rb, gk))

  proceed = str(input("""
> Press enter to continue
"""))

  if proceed == 'y':
    replit.clear()
  else:
    replit.clear()
    main_menu()

#This function does the transfer market stuff
def transfer_market (coins, gk, rb, rcb, lcb, lb, rcm, cm, lcm, rw, st, lw):
  #Gets your goalie

  #Chooses goalkeeper number
  goalkeeper_number = random.randrange (0, 24+1)
  #Chooses goalkeeper
  goalkeeper = goalkeepers [goalkeeper_number]
  #Pulls goalkeeper rating
  global goalkeeper_rating
  goalkeeper_rating = goalkeeper_ratings [goalkeeper_number]
  #Generates goalkeeper price
  goalkeeper_price = 15 * (goalkeeper_rating * 0.5)

  replit.clear()
  print (("""Welcome to the transfer market. You have %d coins. Your avaliable players are:
  """)%(coins))

  #Lists stuff for goalkeeper
  want_goalkeeper = str(input((("""Position: Goalkeeper
  Name: %s
  Rating: %d
  Price: %d

  Do you want him (press 1 to sign): """)%(goalkeeper, goalkeeper_rating, goalkeeper_price))))

  if want_goalkeeper == '1' and coins < goalkeeper_price:
    print ("Sorry. You can't get him.")
  else:
    coins = coins - goalkeeper_price
    print (("You have just signed %s. You have %d coins remaining.")%(goalkeeper, coins))

#Main menu
def main_menu():
  gk = goalkeeper_rating
  print (("You have %d coins.")%(coins))
  next_action = str(input("""
> Your Lineup (1)
> Transfer market (2)
> Your next match (3)

Press 1, 2, or 3 to continue: """))
  if next_action == '1':
    print_lineup (club_name, gk, rb, rcb, lcb, lb, rcm, cm, lcm, rw, st, lw)
  if next_action == '2':
    transfer_market (coins, gk, rb, rcb, lcb, lb, rcm, cm, lcm, rw, st, lw)


#Defines your clubs name
replit.clear()

club_name = str(input("> What is your clubs name: "))
time.sleep(1.2)
replit.clear()

#Generates your players original ratings
gk = random.randrange(60, 70+1)
rb = random.randrange(60, 70+1)
rcb = random.randrange(60, 70+1)
lcb = random.randrange(60, 70+1)
lb = random.randrange(60,70+1)
rcm = random.randrange(60,70+1)
cm = random.randrange(60,70+1)
lcm = random.randrange(60, 70+1)
rw = random.randrange(60, 70+1)
st = random.randrange(60, 70+1)
lw = random.randrange(60,70+1)

#Prints lineup
print_lineup (club_name, gk, rb, rcb, lcb, lb, rcm, cm, lcm, rw, st, lw)

proceed = str(input("""
> Press enter to continue
"""))

if proceed == 'y':
  replit.clear()
else:
  replit.clear()

main_menu()

Solution

  • (Just turned my comment into an answer, since I realised that this might actually be what you are asking for)

    State modeling for large applications is never simple, so I am a bit unsure on what you mean by "the rest of the project".

    However it seems like what you are asking for is how you return values from a functions scope to the calling scope?

    When you modify coins in your function like here coins = coins - goalkeeper_price you only modify the local variable coins in the scope of your function. To return that value to the outer scope you can return the new value.

    So in the end of your function add this line return coins

    Now when you call your function you can set the coins value of the outer scope to the modfied value like this coins = transfer_market (coins, gk, rb, rcb, lcb, lb, rcm, cm, lcm, rw, st, lw)

    If you would like to understand more about why it did not work to just modify the local coins variable in your function, you can read a bit about 'call by value' vs 'call by reference'. https://press.rebus.community/programmingfundamentals/chapter/call-by-value-vs-call-by-reference/ (spoiler: Numbers are parsed by value)