我已经创建了一个代码,在三个不同的函数上迭代字典。
主要功能是这样的:
> 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", } ```
>
> The Three Functions are as follows: ``` def
> lookup_player(brave_roster, name): name = input("Enter the name of
> the player you want to lookup:\n")
> if name in brave_roster:
>
> print("Here are the", [name], "stats:", brave_roster[name]) else:
> print("That player does not exist.")
>
> def add_player_to_dict(brave_roster, name, stats): name =
> input("Enter the name of the player you want to add:\n")
> if name not in brave_roster:
> stats = input("Please add Matt's stats:\n")
> brave_roster[name] = stats
> brave_roster.update([(name, stats)])
> print("Here's", name,"'s stats:\n")
> for name, stats in brave_roster.items():
> print(name, ':', stats) else:
> print("That player is already on our roster.")
>
> def delete_in_dict(brave_roster, name): name = input("Enter the name
> of the player you want to remove:\n") if name in brave_roster:
> del brave_roster[name] else:
> print("\n uh typo?", name, "does not play for us")
>
> 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", }
> choice = input("Please type your choice number:") if (choice == "1"):
> lookup_player(brave_roster) elif (choice == "2"):
> add_player_to_dict(brave_roster) else:
> (choice == "3")
> delete_in_dict(brave_roster) print("\nThanks for using my Braves Stats.")
>
> ```
>
> Essentially from these three functions and the main code I'm supposed
> to use inputs to look up, update, or delete a roaster of players from
> a baseball team.
>
> I have to make sure the earlier code runs in the test codes. However,
> I keep getting errors such as:
>
> > **FAIL: test_add_player_duplicate (__main__.TestDictFunctions.test_add_player_duplicate)
> > ---------------------------------------------------------------------- Traceback (most recent call last): line 106, in
> > test_add_player_duplicate
> > self.assertEqual(actual, expected) AssertionError: {'Austin Riley': 'AB: 615, R: 90, H: 168,[80 chars]214'} != None**
What am I doing wrong?
Here is the test file:
import unittest class TestDictFunctions(unittest.TestCase):
def test_search_player_success(self):test_dict = {“Austin Riley”:“AB:615,R:90,H:168,HR:38,AVG:0.273”} actual = test_dict[“Austin Riley”] expected = lookup_player(test_dict,“Austin Riley”)self.assertEqual(actual,expected)
def test_search_player_no_result(self):test_dict = {“Austin Riley”:“AB:615,R:90,H:168,HR:38,AVG:0.273”} actual =“N/A”expected = lookup_player(test_dict,“罗纳德Acuna”)self.assertEqual(actual,expected)
def test_add_player_sucess(self):test_dict = {“Austin Riley”:“AB:615,R:90,H:168,HR:38,AVG:0.273”}实际= {“Austin Riley”:“AB:615,R:90,H:168,HR:38,AVG:0.273”,“罗纳德Acuna”:“AB:467,R:71,H:124,HR:15,AVG:0.266”} expected = add_player_to_dict(test_dict,“罗纳德Acuna”,“AB:467,R:7 ``` 1,H:124,HR:15,AVG:0.266英寸)
self.assertEqual(actual, expected)
def test_add_player_duplicate(self):test_dict = {“Austin Riley”:“AB:615,R:90,H:168,HR:38,AVG:0.273”}实际= {“Austin Riley”:“AB:615,R:90,H:168,HR:38,AVG:0.273”,“Austin Riley(2)":“AB:350,R:20,H:120,HR:5,AVG:0.214”} expected = add_player_to_dict(test_dict,“Austin Riley”,“AB:350,R:20,H:120,HR:5,AVG:0.214”)self.assertEqual(actual,expected)
def test_delete_player_sucess(self):test_dict = {“Austin Riley”:“AB:615,R:90,H:168,HR:38,AVG:0.273”} expected = {} actual = delete_in_dict(test_dict,“Austin Riley”)self.assertEqual(actual,expected)
def test_delete_word_no_result(self):test_dict = {“Austin Riley”:“AB:615,R:90,H:168,HR:38,AVG:0.273”} expected = test_dict actual = delete_in_dict(test_dict,“Shohei Ohtani”)self.assertEqual(actual,expected)
1条答案
按热度按时间4xrmg8kj1#
您得到的错误是因为测试代码向add_player_to_dict函数传递了三个参数,但函数定义只接受一个参数(brave_roster)。
要修复此错误,您应该修改函数定义以接受附加参数player_name和player_stats,如下所示:
然后,在测试代码中,您可以修改预期变量,以包含新的玩家名称和统计信息:
调用函数时,传递brave_roster、player_name和player_stats参数:
通过这些更改,测试代码应该可以无错误地运行。
另外,请注意,在delete_in_dict函数中,您应该在删除播放器之后添加return brave_roster语句,以便将修改后的字典返回到调用代码。