Вы находитесь на странице: 1из 8

Final (Python)

Chapter 12 Problem 3
# Program that has 10 account ID's in which they can check balance, withdraw,
# deposit, and go back to main menu.
class Account:
# Initiate the class account
def __init__(self, id = 0, balance = 100, annualInterestRate = 0):
self.__id = id
self.__balance = balance
self.__annualInterestRate = annualInterestRate
# Get the ID
def getId(self):
return self.__id
# Get the current balance of the account
def getBalance(self):
return self.__balance
# Get the annual interest rate
def getAnnualInterestRate(self):
return self.__annualInterestRate
# Set the id for the account
def setId(self, id):
self.__id = id
# Set the balance of the account
def setBalance(self, balance):
self.__balance = balance
# Set the annual interest rate
def setAnnualInterestRate(self, rate):
self.__annualInterestRate = rate
# Get the monthly interest rate
def getMonthlyInterestRate(self):
return self.__annualInterestRate/ 12

# Get the monthly interest


def getMonthlyInterest(self):
return self.__balance * self.getMonthlyInterestRate() / 100
# Deposit money to the account
def deposit(self, amount):
print ('Deposited %s to account ID = %s' % (amount, self.getId()))
self.__balance = self.__balance + amount
# Withdraw money from the account
def withdraw(self, amount):
print ('Withdrew %s to account ID = %s' % (amount, self.getId()))
self.__balance = self.__balance - amount
# Java to string method for python to print everything out
def __str__(self):
return 'ID = %s, balance = %s, monthly interest rate = %s, monthly interest = %s' %
(self.getId(), self.getBalance(), self.getMonthlyInterestRate(), self.getMonthlyInterest())

#a = Account(1122, 20000, 4.5)


#print(a)
#a.deposit(2500)
#print(a)
#a.withdraw(3000)
#print(a)
# Create 10 account class object
accountList = [Account(i, 20000, 4.5) for i in range(10)]
while(True):

id = int(raw_input("Enter an account id: "))


print('\n')
print "Main Menu"
while(True):
print "1: Check Balance"
print "2: Withdraw"

print "3: Deposit"


print "4: Exit"
input = int(raw_input("What would you like to do today? "))
if(input == 1):
print(accountList[id])
elif(input == 2):
value = eval(raw_input("How much would you like to withdraw? "))
accountList[id].withdraw(value)
elif(input == 3):
value = eval(raw_input("How much would you like to deposit? "))
accountList[id].deposit(value)
elif(input == 4):
print "Returning to Main Menu"
break

Chapter 13 Problem 8
# Program that encodes a file by adding 5 to every byte in the file. Encrypted version of
# the input file is saved to the output file

def main():
# Get the input file
f1 = raw_input("Enter a source filename: ").strip()
# Get the output file
f2 = raw_input("Enter a target filename: ").strip()
# Open files for input
infile = open(f1, "r")
s = infile.read() # Read all from the file
newS = ""
# Encode file by adding 5 to every byte
for i in range(len(s)):
newS += chr(ord(s[i]) + 5)
infile.close() # Close the input file
outfile = open(f2, "w")
file = outfile
print newS, file # Write to the file
print "Encryption Completed"
outfile.close() # Close the output file
main()

Chapter 13 Problem 9
# Program that decodes a file by subtracting 5 to every byte in the file. Decrypted version of the
input file is saved to the output file
def main():
# Get the input file
f1 = raw_input("Enter a source filename: ").strip()
# Get the output file
f2 = raw_input("Enter a target filename: ").strip()
# Open files for input
infile = open(f1, "r")
s = infile.read() # Read all from the file
newS = ""

# Decode file by subtracting 5 to every byte


for i in range(len(s)):
newS -= chr(ord(s[i]) + 5)
infile.close() # Close the input file
outfile = open(f2, "w")
file = outfile
print newS, file # Write to the file
print "Decryption Completed"
outfile.close() # Close the output file
main()

Chapter 14 Problem 2
# Program that counts and prints the number/s that appear the most.
def main():
s = raw_input("Enter the numbers: ").strip()

numbers = [int(x) for x in s.split()]


#print "numbers: ", numbers
dictionary = {} # Create an empty dictionary
for number in numbers:
#print "for loop number and numbers ", number, numbers
if number in dictionary:
dictionary[number] += 1
#print dictionary[number]
else:
dictionary[number] = 1
# print dictionary[number]
maxCount = max(dictionary.values())
#print "maxCount: ", maxCount
pairs = list(dictionary.items())
#print "pairs: ", pairs
items = [[x, y] for (x, y) in pairs] # Reverse pairs in the list
#print "items ", items
print "The numbers with the most occurrence are "
# Count the number/s that appear the most
for (x, y) in pairs:
if y == maxCount:
print x
main()

Вам также может понравиться