January 20th, 2013, 03:16 AM

Evaluate my code[GAME] :)
I'd like to make a game where everyone posts their code and have you evaluate it.
You could post your own code to be evaluated and change other people's code. I think this is a great opportunity for everyone to become better programmers!
Code:
"""This program displays output of the total of a book order. I need to figure out the total cost for the order of 60 books. pr0x1mity wrote this program from a mathematical word problem."""
number_of_books= 60 # Total number of books
full_price = 24.95 # Full price of the book
bookstore_discount = .40 * 24.95 # Bookstores receive a 40% discount
initial_shipping = 3 # It costs $3 dollars to ship a book
multiple_shipping = .75 # It costs 75 cents to ship each additional book
discounted_book = full_price  bookstore_discount
total_order = discounted_book * number_of_books
discounted_shipping = multiple_shipping * 59 + initial_shipping
total_cost = total_order + discounted_shipping
print(total_cost)
January 20th, 2013, 04:17 AM

I like your idea for a gameisn't code review and improvement exactly what open source software is about?
discounted_shipping = multiple_shipping * 59 + initial_shipping
59 is a magic number. Replace it with
(number_of_books1)
[code]
Code tags[/code] are essential for python code and Makefiles!
January 20th, 2013, 12:52 PM

Originally Posted by b49P23TIvg
I like your idea for a gameisn't code review and improvement exactly what open source software is about?
discounted_shipping = multiple_shipping * 59 + initial_shipping
59 is a magic number. Replace it with
(number_of_books1)
I looked at open source projects, but they all look too complicated... I don't know where to start
I replaced it with number_of_books1 and I got a different result. 945.2 the previous result was 945.45. It shouldn't change anything, right? It doesn't make sense to me.
Wait, I feel stupid. I didn't see the () wrapped around the variable.
January 21st, 2013, 04:30 PM

I can always use more help to improve so this game is a fun idea
Ok I modified your code to make it into a function. Why? Because I think functions are much more modular and adaptable. It makes it possible to test your function with different values.
Also, bookstore_discount is now ".40" instead of ".40*24.95". Instead you can use bookstore_discount*full_price (later in the code) to get the same result.
At the bottom, this code calls the new bookTotal() function twice to show how it works.
Code:
"""This program displays output of the total of a book order.
I need to figure out the total cost for the order of 60 books.
pr0x1mity wrote this program from a mathematical word problem."""
def bookTotal(number_of_books=60,full_price=24.95,bookstore_discount =.40,
initial_shipping=3,multiple_shipping=.75):
discounted_book = full_price  (bookstore_discount*full_price)
total_order = discounted_book * number_of_books
discounted_shipping = multiple_shipping * (number_of_books1) + initial_shipping
return total_order + discounted_shipping
print bookTotal() #Normal book total
print bookTotal(40,9.99,.2) #40 books at $9.99 with only .2 discount
One code I really want to shorten and optimize is a hitTest I'm using in a game. It's used so often in the game that every unnecessary calculation is a problem. Basically, it has two rectangles and returns the side that the first rectangle is getting on (in the form of integers). b4 already saw this project
See if you can shorten the code or make it more efficient without changing its purpose:
Code:
def hitCorner(loc1,loc2,size2):
if loc1.x>loc2.x and loc1.x<loc2.x+size2.x:
if loc1.y>loc2.y and loc1.y<loc2.y+size2.y:
return True
def hitTest2(loc1,size1,loc2,size2,output=0):
if loc1.x+size1.x>=loc2.x and loc1.x<=loc2.x+size2.x:
if loc1.y+size1.y>=loc2.y and loc1.y<=loc2.y+size2.y:
output=1
if output:
#Rectangle alert
if size2.x!=size2.y:
c1=hitCorner(loc1,loc2,size2)
c2=hitCorner(Point(loc1.x,loc1.y+size1.y),loc2,size2)
c3=hitCorner(Point(loc1.x+size1.x,loc1.y+size1.y),loc2,size2)
c4=hitCorner(Point(loc1.x+size1.x,loc1.y),loc2,size2)
if c1 and c2: return 2#l
elif c2 and c3: return 3#t
elif c3 and c4: return 1#r
elif c4 and c1: return 4#b
#Calculate origins
ox1=loc1.x+size1.x/2
oy1=loc1.y+size1.y/2
ox2=loc2.x+size2.x/2
oy2=loc2.y+size2.y/2
#Difference between origins to locate the direction
difx=ox1ox2
dify=oy1oy2
if difx<=0: output1=1#hit from r
else: output1=2#hit from l
if dify<=0: output2=1#hit from t
else: output2=2#hit from b
#check which is most important
if dify<0:dify*=1
if difx<0:difx*=1
if difx>dify: output=output1
else: output=output2+2
#output=output2+2
return output
January 22nd, 2013, 01:16 AM

Elksan, thanks for your input. I'm reading "Think Python" and I'm on the last exercise of the second chapter. The third chapter is all about functions. I hope that I become a proficient programmer when I finish the book. I've never programmed before, but I do speak four languages fluently