想你的整个代码后,我建议你在这里做的是切换你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()时都会覆盖它)。 –