So I am working on a project which basically is to program a Monopoly. I want to be able for a player to pay X amount to the bank and have the bank properly receive and add that amount to its own. I am working the player on a Tkinter interface in a different class to the bank. I have done the following:
In Class Card1, import the Bank class from bank.py
Program a function pay_bank() where I get the amount entered in the Entry field and discount it from the player's available money and send it to bank.
-Create a function receive_payment() in the Bank class where it receives the player's money and adds it to its own.
For some reason, it is not working so I need your help. Code below:
Class Card1:
def __init__(self):
self.amount = 1500
self.properties = {}
#nested functions to handle interface and it's events
def manage_player1_card(self):
def pay_bank():
to_bank = int(payBox.get())
if self.amount > to_bank:
payBox.delete(0, END)
self.amount -= to_bank
Bank().receive_payment(to_bank)
class Bank:
def __init__(self):
self.bank_total = 14580
def receive_payment(self, pay):
self.bank_total += pay
*Indentation might look wrong due to copy-pasting but it is just fine in my code. What do you see I am doing wrong?
Basically, the self.bank_total amount is not adding up every time I enter the amount. For example: if I enter 500, it should go up to 15180, but it stays the same at 14580. I have debugged but it doesn't change. How can I fix this? Thanks!
Try something like this:
def pay_bank(bank: Bank):
...
bank.receive_payment(to bank)
That way you are passing the bank as a parameter.
Related
need help about my code (python):
class Account:
def __init__(self, money):
self.money= money
def __str__(self):
return f'Money in the bank: {self.money} dollar'
def withdraw(self,withdraw):
self.withdraw = withdraw
money = self.money-self.withdraw
return money
print(Account.withdraw(20000,1000))
What I want from code above is the code will print my remaining money (19000), but I always got error
'int' object has no attribute 'withdraw'.
I have tried a lot of things for 4 hours but got no satifying result.
This is my first question in this forum, i am sorry if the formatting is not right.
Thank you in advance :)
Below I have made some small changes in your code and it is working as you expect.
Defined new variable withdrawMoney to track the withdraw amount.
Some changes in string format.
Returned the updated amount every time.
class Account:
def __init__(self, money):
self.money= money
self.withdrawMoney = 0
def __str__(self):
return "Money in the bank: {} dollar".format(self.money)
def withdraw(self,withdrawMoney):
self.withdrawMoney = withdrawMoney
self.money = self.money-self.withdrawMoney
return self.money
acc = Account(20000)
print(acc.withdraw(1000)) # 19000
print(acc.withdraw(1000)) # 18000
I am new to Python and trying to make a function for an assignment that will transfer money from a checking to a savings account,. We were given this initial code:
class portfolio:
def __init__(self):
self.checking = {}
self.saving = {}
self.credit = {}
self.stock = {}
And then the start of this code to make the function to transfer the money:
def invest_in_savings_account(self, account_id_savings, amount, account_id_checking):
I've tried numerous code, but none will pass the test cases. Can someone explain to me why this won't work?
def invest_in_savings_account(self, account_id_savings, amount, account_id_checking):
try:
self.checking[account_id_checking] -= amount
self.saving[account_id_savings] += amount
except:
return None
If the account id doesn't exist or if there are no funds in the checking account, the function is to do nothing.
Any suggestions would be greatly appreciated! I've worked on this all day and haven't been able to solve it.
This is the test case it must pass:
myportfolio.invest_in_savings_account('discover_saving_3785', 1000, 'discover_7732')
if (myportfolio.saving == {'chase_saving_4444': 0, 'discover_saving_3785':1000}) and (myportfolio.checking == {'chase_6688': 100, 'discover_7732':5500}):
print('Pass')
else:
print('Fail')
There are a few things going on here, most simple first, always start a class with a capital letter, it isn't necessary but it's good practice:
def class Portfolio:
After reading your comments it appears you need this to work without money in the accounts. You can write the code but you won't be able to test it, so really you need to make functions to create accounts and to add / remove money. If you haven't created any accounts your dictionaries will always come back empty. If you don't put money into them then the function will never do anything, I'm confused as to how you would go about moving something that doesn't exist.
Try something like this:
def create_checking_account(self,account_id):
self.checking[account_id] = None
def add_funds_checking(self,account_id,amount):
self.checking[account_id] += amount
def invest_in_savings(self, account_id_savings, amount, account_id_checking):
if self.checking[account_id_checking] >= amount:
self.checking[account_id_checking] -= amount
self.savings[account_id_savings] += amount
else:
print('error')
In the below code if I create an instance of account as accnt and write accnt.owner, the program returns the memory location and not the value at the location. If i use str() method for the owner and balance I am able to obtain accnt.owner and accnt.balance values simultaneously.Is it possible to access both of them separately by just typing
accnt.owner) \n
accnt.balance
without using print function?
class Account():
def __init__(self,owner,balance):
self.owner = owner
self.balance = balance
accnt = ("Jose",100)
acct.owner----> Should output Jose
acct.balance---->Should output 100
You are making a mistake, you have to indicate what class you are trying to instantiate:
class Account():
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
accnt = ("Jose", 100) # <------------- This is wrong
accnt = Account("Jose",100) # <------- This is right
print(acct.owner)
print(acct.balance)
When you call a variable from the console, it gets printed, but from a code script, you must use print().
I not very sure how to phrase the question subject...
I have 3 files laid out:
bank.py:
class Bank(object):
def __init__(self, money):
self.money = money
def currentMoney(self):
print "You currently have $%d" %self.money
def useMoney(self, money_use):
self.money = self.money - money_use
print "You used $%d" %money_use
self.currentMoney()
def getMoney(self, money_get):
self.money = self.money + money_get
print "You received $%d" %money_get
self.currentMoney()
event.py:
class Event(object):
def Event1(self):
print "Your dad needs money. Will you give him?"
decision = raw_input("Yes or No")
if decision == "Yes":
Bank.useMoney(500)
elif decision == "No":
print "Your father is sad"
else:
print "I do not know what are you talking about"
main.py:
import bank
import event
Bank = bank.Bank(1000)
Event = event.Event()
Event.Event1()
When I execute the code. I get the following error:
NameError: global name 'Bank' is not defined
Basically, what I would like to do is to use event.py to create a series of events that will affect the money, and I can use main.py to run different series of events.
Would you be able to enlighten me how to do this? Thank you!
As mentioned in the other answer, you need to import Bank in event.py
from bank import Bank
Having said that, looking at the code in event.py, you're going to run into another error on the line:
Bank.useMoney(500)
As useMoney() takes self as the first argument, so it needs to be run on a Bank instance rather than on the class itself (in Java-type speak, useMoney is an instance method, not a static method).
I think what your intent is is to have a Bank instance be contained inside Event, and then call useMoney on that whenever Event1 is called.
You need to import bank in event.py, and use
if decision == "Yes":
bank.Bank(...)
or you could use at the beginning of your event.py:
from bank import Bank
class Event(object):
...
Otherwise, Python cannot know that the Bank object is defined in the bank module when it loads the event module.
I have a homework assignment that's really baking my noodle. It involves an elevator simulation that takes user inputs for the number of floors and the number of people using the elevator. the people's starting floor and destination floors are random numbers within the floors.
I realize that my code is very sparse and that there's quite a few gaps, but I really don't know where to go from here.
I need help within the building class, such as how to make the run() and output() sections work. any other tips would be greatly appreciated and helpful. Note that i am not looking for someone to do the code for me, but to kind of hold my hand and tell me which way to go. Classes seem to be completely mystifying to me.
import random
floors=raw_input('Please enter the number of floors for the simulation:')
while floors.isalpha() or floors.isspace() or int(floors) <=0:
floors=raw_input('Please re enter a digit for number of floors:')
customers=raw_input('Please enter the number of customers in the building:')
while customers.isalpha() or customers.isspace() or int(customers) <0:
customers=raw_input('Please re enter a digit for number of customers:')
count = 1
class building:
def num_of_floors():
num_of_floors = floors
def customer_list():
customer_list = customers
def run(self):
def output(self):
print elevator.cur_floor
class elevator:
def num_of_floors():
building.num_of_floors
def register_list():
register_list = []
def cur_floor(building):
cur_floor = 1
def direction(self):
if elevator.cur_floor == 1:
direction = up
if elevator.cur_floor == floors:
direction = down
def move(self):
if elevator.direction == up:
cur_floor +=1
if elevator.direction == down:
cur_floor -=1
def register_customer(self, customer):
register_list.append(customer.ID)
def cancel_customer (self, customer):
register_list.remove(customer.ID)
class customer:
def cur_floor(customer):
cur_floor = random.randint(0,int(floors))
def dst_floor(customer):
dst_floor = random.randint(0,int(floors))
while dst_floor == cur_floor:
dst_floor = random.randint(0,int(floors))
def ID():
cust_id = count
count+=1
def cust_dict(cust_id,dst_floor):
cust_dict = {cust_id:dst_floor}
def in_elevator():
in_elevator = 0
if customer.ID in register_list:
in_elevator = 1
def finished():
if customer.ID not in register_list:
pass
You need to understand the self
parameter to all methods.
You need to understand __init__,
the constructor.
You need to understand self.varible
for your member variables.
You need to understand how to setup a
main function.
You need to understand how to
return a value from a function or
method.
You need to understand how to assign to global variables from within a function or method.
Maybe your building class should start like this.
class building:
def __init__(self, floors, customers):
self.num_of_floors = floors
self.customer_list = customers
self.elevator = elevator()
You should definately spend some time on Python Tutorial or Dive into Python.
The first parameter of every method is a reference to the object and is usually called self. You need it to reference instancemembers of an object.
Second, referencing global variables from inside a class is considered a bad idea. You can better pass them to a class via the constructor or parameters.