This code is only for python. this is a game- transfer rings there are 3 problems which you are solving: A: failed transfer - make sure the progam dont take more than what the user inputed by raising an exception
This code is only for python. this is a game- transfer rings
there are 3 problems which you are solving:
A: failed transfer - make sure the progam dont take more than what the user inputed by raising an exception
B: codE the trade_money function
C: if sonic doesnt have as much as the user inputed, there will be an error pop up 'ERROR TRADE FAILED'. make it so the progam can run normally rather than turning it down.
CODE:
class user:
def __init__(self, name, initial_money):
self.name = name
self.money = initial_money
def give_money(self, amount):
self.money = self.money + amount
def take_money(self, amount):
# Part A: Raise an exception as appropriate
self.money = self.money - amount
def print_users(users):
total_money = 0
for user in users:
print(f'{user.name:s} has {user.money:d} rings')
total_money = total_money + user.money
print(f'There are a total of {total_money:d} rings')
# B: code the trade_money function
sonic = user('sonic', 80)
tails = user('tails', 20)
print_users([sonic, tails])
another_transfer = 'y'
while another_transfer == 'y':
num_money = int(input('Enter number of coins to transfer from Mario to Luigi: '))
# C: Print an appropriate message if an exception is encountered
trade_money(num_money, sonic, tails)
print_users([sonic, tails])
another_transfer = input('Do you want another trade? (y/n): ')
Step by step
Solved in 2 steps with 2 images