如何将三个不同的函数调用到一个主函数



这个程序的目的是创建三个调用主函数的函数。

下面是主函数:

def main():
# initial roster
brave_roster = {
"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",
"Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266",
}
print("t ***  Braves Stats!  ***n")
print("Welcome to My Braves Stats! What can I do for you?n")
print('  1. Search for a player')
print('  2. Add a new player')
print('  3. Remove a player')
choice = int(input("Please type your choice number:"))

函数一:这个函数在字典中查找特定的单词。

def lookup_player(brave_roster, name, choice):

player_look = input("Enter the name of the player you want to look up:")
#while brave_roster == True:
for name in brave_roster:
if choice == 1:
#print(brave_roster[name])
print("Here are Austin's stats:", brave_roster[name])
elif choice == 1:
#print(player_look)
print("Here are Ronald's stats:", brave_roster[name])
else:
choice == False
print("N/A")

return choice, name

def add_player_to_dict(brave_roster,new_player_add):

  playad = input("Enter the name of the player you want to add:")

new_player_add = {}
for i in range(1):
player_name = input()
player_stats = input()
new_player_add[player_name] = player_stats
while True:
if choice == 2:
print(playad,new_player_add)
brave_roster.update(new_player_add)
else: 
choice == False
print("That player already exists.")


return new_player_add

函数三从字典中删除键:

def delete_in_dict(brave_roster):
d_player = input("Enter the name of the player you want to remove:")
x = input()
while True:
if choice == 3:
print(d_player)
for k in list(brave_roster.keys()):
if brave_roster[k] == 1:
del brave_roster[k]
elif choice == 3:
for k in list(brave_roster.keys()):
if brave_roster[k] == 2:
del brave_roster[k]
else:
print("Uh typo?", x, "Does not play for us:" )
if __name__ == '__main__':
choice = main()
brave_roster = {
#"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",
#"Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266",
}
player_look = lookup_player(brave_roster, name, choice)
print("Thanks for using my Braves Stats.")

我一直在尝试多种方法来让这些功能工作,但我仍然站在原地。

如果我正确理解了你的问题,你希望你的程序的用户选择执行哪个操作,然后在完成后,它再次要求用户选择一个操作或退出。

既然如此,我建议你这样做:

"""this is the main function, it will call the chosen action"""
def main(brave_roster):
# placing the selection inside a loop so the user can continue or leave
while True:
print("nWhat can I do for you?")
print("  1. Search for a player")
print("  2. Add a new player")
print("  3. Remove a player")
choice = input("Please type your choice number:")
# depending on the choice, call the corresponding function
if (choice == "1"):
lookup_player(brave_roster)
elif (choice == "2"):
add_player_to_dict(brave_roster)
elif (choice == "3"):
delete_in_dict(brave_roster)    
else:
# or else, press enter to exit
break
"""lookup a player, otherwise fail if the name is not present"""
def lookup_player(brave_roster):
name = input("Enter the name of the player you want to lookup:n")
# no need to hard code checking for each player
# just check the the input name is in the dict
if name in brave_roster:
print("Here are the player's stats:", brave_roster[name])
else:
print("That player does not exist.")

"""add a player, otherwise fail if the name is already present"""
def add_player_to_dict(brave_roster):
name = input("Enter the name of the player you want to add:n")
# only add if the name is not already there
if name not in brave_roster:
stats = input("Enter the player statsn")
# this is how you add a new item to the dict
brave_roster[name] = stats
else:
print("That player already exists.")
"""delete a player, otherwise fail if the name is not present"""
def delete_in_dict(brave_roster):
name = input("Enter the name of the player you want to remove:n")
if name in brave_roster:
del brave_roster[name]
else:
print("nThat player does not exist.")
# now, print the greeting, initialize the player database and
# call the main function
if __name__ == '__main__':
print("t ***  Braves Stats!  ***n")
print("Welcome to My Braves Stats!")
brave_roster = {
"Austin Riley": "AB: 615, R: 90, H: 168, HR: 38, AVG: 0.273",
"Ronald Acuna": "AB: 467, R: 71, H: 124, HR: 15, AVG: 0.266",
}
main(brave_roster)
print("nThanks for using my Braves Stats.")

最新更新