#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0

    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)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,700
    Rep Power
    480
    I like your idea for a game---isn'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_books-1)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I like your idea for a game---isn'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_books-1)
    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_books-1 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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    43
    Rep Power
    2
    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_books-1) + 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=ox1-ox2
    		dify=oy1-oy2
    		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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    5
    Rep Power
    0
    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

IMN logo majestic logo threadwatch logo seochat tools logo