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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Topic Video
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.
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

 

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

    def give_coins(self, amount):
        self.coins = self.coins + amount
    
    def take_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 2 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY