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. Example Runs 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 Your code should execute as closely as possible to the example runs above. To check for correctness, ensure that your program gives the same outputs as in the examples, as well as trying it with other inputs. Your Solution: class Player: def__init__(self, name, initial_coins): self.name = name self.coins = initial_coins defgive_coins(self, amount): self.coins = self.coins + amount deftake_coins(self, amount): # Part B: Raise an exception as appropriate self.coins = self.coins - amount def print_players(players): total_coins = 0 for player in players: print(f'{player.name:s} has {player.coins:d} coins') total_coins = total_coins + player.coins print(f'There are a total of {total_coins:d} coins') # Part A. Write your transfer_coins function here mario = Player('Mario', 80) luigi = Player('Luigi', 20) print_players([mario, luigi]) another_transfer = 'y' while another_transfer == 'y': num_coins = int(input('Enter number of coins to transfer from Mario to Luigi: ')) # Part C. Print an appropriate message if an exception is encountered transfer_coins(num_coins, mario, luigi) print_players([mario, luigi]) another_transfer = input('Perform another transfer? (y/n): ')

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter6: Looping
Section: Chapter Questions
Problem 4GZ
icon
Related questions
Question

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.
 
 
 
 
 

Example Runs

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

Your code should execute as closely as possible to the example runs above. To check for correctness, ensure that your program gives the same outputs as in the examples, as well as trying it with other inputs.

 
 
 
 
 

Your Solution:

 

 

class Player:
def__init__(self, name, initial_coins):
self.name = name
self.coins = initial_coins

defgive_coins(self, amount):
self.coins = self.coins + amount
 
deftake_coins(self, amount):
# Part B: Raise an exception as appropriate
self.coins = self.coins - amount


def print_players(players):
total_coins = 0
for player in players:
print(f'{player.name:s} has {player.coins:d} coins')
total_coins = total_coins + player.coins
print(f'There are a total of {total_coins:d} coins')


# Part A. Write your transfer_coins function here


mario = Player('Mario', 80)
luigi = Player('Luigi', 20)
print_players([mario, luigi])

another_transfer = 'y'
while another_transfer == 'y':
num_coins = int(input('Enter number of coins to transfer from Mario to Luigi: '))

# Part C. Print an appropriate message if an exception is encountered
transfer_coins(num_coins, mario, luigi)

print_players([mario, luigi])
another_transfer = input('Perform another transfer? (y/n): ')
Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Linux
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,