You are on page 1of 4

YASH RUGHWANI

0801IT211098
ASSIGNMENT 06

Ques1) Create a class to represent a single die that can have any positive integer number of sides.
This kind of die might be used when playing role-playing games (RPGs).
Program:
import random
print(‘0801IT211098 Yash Rughwani’)
class Die:
def __init__(self, sides):
self.sides = sides

def roll(self):
return random.randint(1, self.sides)
d6 = Die(6) # a six-sided die
d20 = Die(20) # a twenty-sided die
result = d6.roll() # roll the d6 and get a random number between 1 and 6
print('The number appeared on die with 6 sides is: ',result)
result = d20.roll() # roll the d20 and get a random number between 1 and 20
print('The number appeared on die with 20 sides is: ',result)

Output:

Ques2) Write a class to represent an RPG character’s money pouch. Money should be stored as
an integer, with methods for adding money and removing money. The removing method should
take a value as parameter. If there is enough money, the value is removed from the money in the
pouch and True is returned. If there is not enough money, False is returned.
Program:
print('0801IT211098 Yash Rughwani')
class MoneyPouch:
def __init__(self, money=0):
self.money = money
def add_money(self, amount):
self.money += amount
def remove_money(self, amount):
if self.money >= amount:
self.money -= amount
return True
else:
return False
pouch = MoneyPouch(100) # create a pouch with 100 gold coins
pouch.add_money(50) # add 50 more gold coins
pouch.remove_money(75) # remove 75 gold coins
if pouch.remove_money(75):
print("Successfully removed 75 gold coins from the pouch")
else:
print("Could not remove 75 gold coins from the pouch, not enough money")

Output:

Ques3)Implement the multiplication operation for the Matrix class. The trick to implementing
multiplication is to realize you need three, nested loops.
Program:
print('0801IT211098 Yash Rughwani')
class Matrix:
def __init__(self, rows, cols):
self.rows = rows
self.cols = cols
self.data = [[0 for _ in range(cols)] for _ in range(rows)]
def __repr__(self):
return f"Matrix({self.data})"
def __mul__(self, other):
if self.cols != other.rows:
raise ValueError("Number of columns in first matrix must match number of rows in
second matrix")
result = Matrix(self.rows, other.cols)
for i in range(self.rows):
for j in range(other.cols):
dot_product = 0
for k in range(self.cols):
dot_product += self.data[i][k] * other.data[k][j]
result.data[i][j] = dot_product
return result
m1 = Matrix(2, 3)
m1.data = [[1, 3, 2], [8, 7, 6]]
m2 = Matrix(3, 2)
m2.data = [[7, 6], [8, 9], [19, 12]]
result = m1 * m2
print(result)

Output:

Ques4)Write a class Account that stores the current balance, interest rate and account number of
a bank account. Your classshould provide methods to withdraw,deposit and add interest to the
account. The user should only be allowed to withdraw money up to some overdraft limit. If an
account goes overdrawn, there is fee charged.
Program:
print('0801IT211098 Yash Rughwani')
class Account:
def __init__(self, account_number, interest_rate, overdraft_limit, overdraft_fee):
self.account_number = account_number
self.balance = 0
self.interest_rate = interest_rate
self.overdraft_limit = overdraft_limit
self.overdraft_fee = overdraft_fee
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance - amount < -self.overdraft_limit:
print("Transaction failed: You cannot withdraw more than your overdraft limit.")
return False
else:
self.balance -= amount
if self.balance < 0:
self.balance -= self.overdraft_fee
return True
def add_interest(self):
interest = self.balance * self.interest_rate
self.balance += interest
return interest
account = Account("123456789", 0.01, 100, 10)
account.deposit(500)
account.withdraw(700) # should fail due to overdraft limit
account.withdraw(400) # should succeed, with an overdraft fee charged
account.add_interest() # should add interest to the balance
print(f"Account {account.account_number} has a balance of {account.balance}")

Output:

You might also like