想你的整個代碼後,我建議你在這裏做的是切換你SearchPlayer
這一個:
def searchplayer():
print("===============SEARCH by player: Calculate average goals==================")
if len(player_info.keys())==0:
print("you have no players registered")
else:
name = input("Player name : ")
while name not in player_info.keys():
print("Please enter a valid player name:")
name = input("Player name: ")
#print student_info
print ("Average player goals : ", str(sum(player_info[name].values())/3.0))
print()
mainmenu()
一件事,你並沒有問,但你應該添加一個檢查器輸入的用戶從詢問時,通過類型來handling exceptions做出決定如下:
try:
choice=int(input("Enter choice:"))
except:
print("Input must be int from 1-5")
mainmenu()
它是有用的,以防止您的應用程序被誤崩潰當我輸入的字符串不是int。
,如果你不想使用遞歸,你可以這樣做:
proceed = True
def main():
while proceed:
mainmenu()
和改變:
sys.exit()
有:
proceed = False
(我只是選擇了主動關閉sys.exit()
,因爲它產生了一些警告)
併爲您的所有方法起飛mainmenu()
。應該這樣做很好
所以整個代碼應該是這樣的(我不熟悉repl.it不好意思):
#SOLUTION==================FOOTBALL COACH app
#The program allows a user to enter a number of students (their names and test
#scores) and then search for a student, returning their average score for the
#three tests
#1------Create a similar program for a football coach (he wants to store player
#names + goals for 3 matches)
#2 -----main menu that allows for 1. Adding players + goals and 2. Search by
#Player 3. Quit
#3-----When complete, go back and add additional menu options for "View all
#players" and Update". This allows the coach to update the number of goals for
#any given player as well as view all
import sys #note the sys.exit() command will not work without this
player_info={}
proceed = True
def main():
while proceed:
mainmenu()
def mainmenu():
global proceed
print("=====WELCOME to the MAIN MENU=============")
print("""
1..........Add New Players & Goals
2..........Search by Players (return average goals)
3----------Update Player Goals
4----------View All players
5..........Quit
=========================================
""")
try:
choice=int(input("Enter choice:"))
except:
print("Input must be int from 1-5")
mainmenu()
if choice==1:
playerinfo=addplayers()
elif choice==2:
searchplayer()
elif choice==3:
update()
elif choice==4:
viewall()
elif choice==5:
proceed = False
else:
print("You must make a valid choice - 1, 2 or 3")
def viewall():
for keys, values in player_info.items():
print(keys, values)
print()
def update():
playername=input("Which player's goals do you wish to update?:")
m1=int(input("Match 1 new entry:"))
m2=int(input("Match 2 new entry:"))
m3=int(input("Match 3 new entry:"))
if playername in player_info:
#myDict["A"] = "Application"
player_info[playername]="Boo"
player_info[playername]={"Match 1 goals":m1,"Match 2 goals":m2,"Match 3 goals":m3}
def addplayers():
global player_info #this needs to be declared as a global variable so it can be used by searchplayer()
player_info= {} #create a dictionary that stores the player name: player goals
num_players = int(input("Please enter number of players you wish to enter:"))
print ("You are entering %s players" %num_players)
player_data = ['Match 1 goals : ', 'Match 2 goals : ', 'Match 3 goals : ']
for i in range(0,num_players):
player_name = input("Enter Player Name :")
player_info[player_name] = {}
for entry in player_data:
player_info[player_name][entry] = int(input(entry)) #storing the marks entered as integers to perform arithmetic operations later on.
print()
def searchplayer():
print("===============SEARCH by player: Calculate average goals==================")
if not player_info:
print("you have no players registered")
else:
name = input("Player name : ")
while name not in player_info.keys():
print("Please enter a valid player name:")
name = input("Player name: ")
#print student_info
print ("Average player goals : ", str(sum(player_info[name].values())/3.0))
print()
main()
希望這有助於
「的錯誤提示player_info不在你鏈接的代碼中定義的「=>不在任何地方定義。 「但它已被聲明爲全局變量」=>在哪裏? –
它是addplayer()中的一個全局變量,注意它在searchplayer()中的工作方式非常好......就我所知,問題與while循環的結構有關。我也需要它完成搜索後返回到主菜單,如果字典是空的 – MissComputing
確實,我錯過了它。請注意,直到調用addplayers()後纔會定義WONT(並且每次調用addplayers()時都會覆蓋它)。 –