For this task, you are to write code that handles the transfer of virtual coins between players in a video game. Instruc
Posted: Fri May 20, 2022 12:16 pm
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:
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: