relates to tranferring coins between players, a Player class has been provided, which supports adding and subtracting coins. Also provided is an interactive loop for testing the transfer of coins between players. Part A You are to write a function called transfer_coins with three parameters: the number of coins to transfer, the player from which coins will be taken (the "giver"), and the player which the coins will be given to (the "receiver"). It should use the appropriate instance methods on each of the
For this task, you are to write code that handles the transfer of virtual coins between players in a video game.
Instructions
As this task relates to tranferring coins between players, a Player class has been provided, which supports adding and subtracting coins. Also provided is an interactive loop for testing the transfer of coins between players.
Part A
You are to write a function called transfer_coins with three parameters: the number of coins to transfer, the player from which coins will be taken (the "giver"), and the player which the coins will be given to (the "receiver"). It should use the appropriate instance methods on each of the player objects to update them as required.
Part B
If you tested your solution thoroughly in the previous part, you probably came across a logic error in the program---there's nothing to stop us from taking more coins from a player than they have! You are to fix this problem by raising an exception instead of completing the transfer of coins. You should do this by raising a ValueError in the appropriate place in the Player class.
Hint: The order of statements in the transfer_coins function is important---neither player should have more or less coins after a failed transfer.
Part C
At this point, the program prevents a transfer occurring if the giver doesn't have enough coins. However, simply crashing a program isn't a very nice user experience. You are to modify your program so that it handles the ValueError and displays !!! Transfer failed !!! to the user. The program should otherwise continue as normal, with the user being asked whether they would like to perform another transfer.
Hint: A little rusty at exception handling? Take a look at the relevant lab for examples.
Requirements
To achieve full marks for this task, you must follow the instructions above when writing your solution. Additionally, your solution must adhere to the following requirements:
- The transfer_coins function must not directly access the coins instance variable of either player.
- You must raise an exception from within the Player class if the giver does not have enough coins to complete a transfer.
- You must specify the appropriate exception type both when raising and handling the exception.
- The transfer_coins function call must be the only thing in your try block.
- A failed transfer must not change the number of coins held by either player.
Run 1
Mario has 80 coins
Luigi has 20 coins
There are a total of 100 coins
Enter number of coins to transfer from Mario to Luigi: 30
Mario has 50 coins
Luigi has 50 coins
There are a total of 100 coins
Perform another transfer? (y/n): n
Run 2
Mario has 80 coins
Luigi has 20 coins
There are a total of 100 coins
Enter number of coins to transfer from Mario to Luigi: 70
Mario has 10 coins
Luigi has 90 coins
There are a total of 100 coins
Perform another transfer? (y/n): y
Enter number of coins to transfer from Mario to Luigi: 60
!!! Transfer failed !!!
Mario has 10 coins
Luigi has 90 coins
There are a total of 100 coins
Perform another transfer? (y/n): y
Enter number of coins to transfer from Mario to Luigi: 5
Mario has 5 coins
Luigi has 95 coins
There are a total of 100 coins
Perform another transfer? (y/n): n
Step by step
Solved in 2 steps with 1 images