python - Answer Error, Only outputting Zero -
i coding in python , cannot seem figure out why when amount of tickets sold entered not calculate full price of tickets sold. appreciated, thanks.
alimit=300 blimit=500 climit=100 aprice=20 bprice=15 cprice=10 ticketsold=1 totalincome=0 def main(): gettickets(alimit) sectionincome=calcincome(ticketsold,aprice) sectionincome+=totalincome print("the theater generated money section "+ str(sectionincome)) gettickets(blimit) sectionincome=calcincome(ticketsold,bprice) sectionincome+=totalincome print("the theater generated money section b "+ str(sectionincome)) gettickets(climit) sectionincome=calcincome(ticketsold,cprice) sectionincome+=totalincome print("the theater generated money section c "+ str(sectionincome)) print("the theater generated "+str(totalincome)+" total in ticket sales.") def gettickets(limit): ticketsold=int(input("how many tickets sold? ")) if (ticketsvalid(ticketsold,limit)==true): return ticketsold else: gettickets(limit) def ticketsvalid(sold,limit): while (sold>limit or sold<0): print("error: there must tickets less "+ str(limit)+" , more 0") return false return true def calcincome(ticketsold,price): return ticketsold*price main()
how debug (small) program
okay let's start top , go line-by-line. there's lot of issues here.
alimit=300 blimit=500 climit=100 aprice=20 bprice=15 cprice=10 ticketsold=1 totalincome=0 these globals since defined them in module scope. that's bad thing. don't it. if they're constants, use caps mention that, should still not global.
def main(): # mentioned in comment, capitals classes # convention, use def main() instead gettickets(alimit) let's stop , @ gettickets() can follow execution
def gettickets(limit): # camelcase not advised per pep8, it's still around # wouldn't worry 1 capitalized ticketsold=int(input("how many tickets sold? ")) # perfect implementation, though prepared users # type forty instead of 40! if (ticketsvalid(ticketsold,limit)==true): return ticketsold # time write `if ___ == true`, stop , realize compare # unnecessary. if ticketsvalid(ticketsold,limit) works well! else: gettickets(limit) # wha-? if tickets aren't valid, we're recursing??! infinite loop. # doing prompt more input if tickets aren't valid? that's bad okay invoked ticketsvalid in there, let's there now...
def ticketsvalid(sold,limit): # capital here! while sold > limit or sold < 0: # should if?? print ("...") return false return true # since have set amount, easier written as: ## def ticketsvalid(sold,limit): ## return 0 < sold < limit # should <=? alright, main--ahem--main....
def main(): ... sectionincome = calcincome(ticketsold,aprice) and hop calcincome
def calcincome(ticketsold,price): return ticketsold*price # why not substitute this??? main again
def main(): ... sectionincome += totalincome # sets sectionincome equal current value of sectionincome # plus current value of totalincome, zero. then whole thing gets repeated down function. there's issue, += adds 0 sectionincome instead of adding sectionincome totalincome!
the better way this!
here's problem. you're trying use functional programming object-oriented tasks. of these kind of issues when new programmers interested in video games think best task learn programming text adventure. unfortunately, best languages text adventures (those implement finite state machine) not beginners start with, it's hard implement well!
in case, should creating objects workload you. python elegantly, it's in beginning tutorials. example, wrote out bit did (defines 3 sections of seating in theater , sells 1 ticket per section)
class section(object): def __init__(self,price,limit): self.price = price self.tickets_sold = 0 self.limit = limit @property def sales(self): return self.price*self.tickets_sold def sell(self,qty=1): if not isinstance(qty,int): raise typeerror("must sell int of tickets") if qty < 1: raise valueerror("must sell positive tickets") qty = min(qty,self.limit-self.tickets_sold) self.tickets_sold += qty # optional print statement user class theater(object): def __init__(self,sections): self.sections = sections @property def sales(self): return sum(section.sales section in self.sections) theater = theater([section(20,300), section(15,500), section(10,100)]) section in theater.sections: section.sell(1) print(theater.sales) the big problem don't know how it. creating object stay constant, throw several instances of around specific attributes precisely approach favor in circumstance.
Comments
Post a Comment