如何将值从一个功能转移到项目的其余部分

问题描述 投票:0回答:1

[我正在编码一个足球比赛,并且一开始硬币被设置为2000,但是每当我购买球员时,硬币都不会调整硬币(重新编码是平台,因此replit.clear()会清除整个屏幕)。

转移市场功能:

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))

整个代码:

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()
python function transfer
1个回答
0
投票

((只是把我的评论变成了答案,因为我意识到这可能实际上就是您要的内容)

大型应用程序的状态建模从来都不是那么简单,因此我不确定“项目的其余部分”是什么意思。

但是您似乎想要的是如何将值从函数范围返回到调用范围?

[如此处coins = coins - goalkeeper_price那样在函数中修改硬币时,仅在函数范围内修改局部变量硬币。要将值返回到外部范围,可以返回新值。

因此,在函数末尾添加此行return coins

现在,当您调用函数时,可以像[coins = transfer_market (coins, gk, rb, rcb, lcb, lb, rcm, cm, lcm, rw, st, lw)这样将外部示波器的硬币值设置为修改后的值

[如果您想了解更多有关为何仅修改函数中的本地硬币变量为什么不起作用的信息,则可以阅读有关“按值调用”与“按引用调用”的内容。https://press.rebus.community/programmingfundamentals/chapter/call-by-value-vs-call-by-reference/(扰流器:数字按值解析)

© www.soinside.com 2019 - 2024. All rights reserved.