def draw_head(settings): "Draws the head" fillcolor(HEAD_COLOR) with restore_state_when_finished(): update_position(HEAD_ORIGIN) right(settings['head_angle']) rectangle_from_center(HEAD_WIDTH, HEAD_HEIGHT)
def draw_arm(settings, which_arm): """ Draws an arm, starting with the upper arm, then the lower arm, and finally the hand """ fillcolor(ARM_COLOR) with restore_state_when_finished(): update_position(ARM_ORIGIN) right(90) shoulder_angle = settings[which_arm + '_arm_shoulder_angle'] right(shoulder_angle) rectangle_from_side_edge(UPPER_ARM_WIDTH, UPPER_ARM_LENGTH) fly(UPPER_ARM_LENGTH) elbow_angle = settings[which_arm + '_arm_elbow_angle'] right(elbow_angle) rectangle_from_side_edge(LOWER_ARM_WIDTH, LOWER_ARM_LENGTH) fly(LOWER_ARM_LENGTH) left(45) rectangle(HAND_WIDTH, HAND_LENGTH)
def draw_leg(settings, which_leg): """ Draws a leg, starting with the upper leg, then the lower leg, and finally the foot. """ fillcolor(LEG_COLOR) with restore_state_when_finished(): update_position(LEG_ORIGIN) right(90) hip_angle = settings[which_leg + '_leg_hip_angle'] right(hip_angle) rectangle_from_side_edge(UPPER_LEG_WIDTH, UPPER_LEG_LENGTH) fly(UPPER_LEG_LENGTH) knee_angle = settings[which_leg + '_leg_knee_angle'] right(knee_angle) rectangle_from_side_edge(LOWER_LEG_WIDTH, LOWER_LEG_LENGTH) fly(LOWER_LEG_LENGTH) left(90) back(LOWER_LEG_WIDTH / 2) rectangle_from_side_edge(FOOT_WIDTH, FOOT_LENGTH)
def cars_drive(): a = 0 helpers.make_car(canvas, top_left=(a - 250, 50), tag='car1') helpers.make_car(canvas, top_left=(a + 550, 300), tag='car2') while a == a: a = 0 while a < 900: time.sleep(0.2) helpers.update_position(canvas, 'car1', x=50, y=0) helpers.update_position(canvas, 'car2', x=-50, y=0) gui.update() a = a + 50 a = 850 while a < 900: time.sleep(0.2) helpers.update_position(canvas, 'car1', x=-50, y=0) helpers.update_position(canvas, 'car2', x=50, y=0) gui.update() a = a - 50
def draw_animation(num_frames, sidelen, color, sleeptime): for i in range(num_frames): if i == num_frames / 4: draw_triangle(sidelen, color) if i == num_frames / 2: update_position(100, 0) draw_triangle(sidelen, color) if i == 3 * num_frames / 4: update_position(200, 0) draw_triangle(sidelen, color) if i == num_frames: update_position(100, 0) draw_triangle(sidelen, color) screen.update() time.sleep(sleeptime) clear()
from tkinter import Canvas, Tk import time import helpers gui = Tk() gui.title('Animation') canvas = Canvas(gui, width=500, height=500, background='white') canvas.pack() ########################## YOUR CODE BELOW THIS LINE ############################## # draw car (and give it a unique tag) helpers.make_car(canvas, top_left=(0, 50), tag='car1') # move car 50 pixels to the right: time.sleep(1) helpers.update_position(canvas, 'car1', x=50, y=0) gui.update() # move car 50 pixels to the right (exact same code): time.sleep(1) helpers.update_position(canvas, 'car1', x=50, y=0) gui.update() # move car 50 pixels to the right (exact same code): time.sleep(1) helpers.update_position(canvas, 'car1', x=50, y=0) gui.update() ########################## YOUR CODE ABOVE THIS LINE ############################## # makes sure the canvas keeps running: canvas.mainloop()
def buy(): """Buy shares of stock""" if request.method == "GET": return render_template("buy.html") else: try: sym = request.form.get("symbol") shares = int(request.form.get("shares")) if not sym or not shares: #If input from has empty fields raise Exception("One or more Input fields were empty") if shares <= 0: #if non negative value was provided raise Exception("Non positive integer provided.") #lookup stock price price = lookup(sym) if not price: raise Exception("Couldnt lookup stock price") else: price = price["price"] #retrieve current users id user_id = session["user_id"] #calculate total price of transaction and check if user has valid credit total_price = price * shares user_credit = float( db.execute("SELECT cash FROM users WHERE id = :user_id", user_id=user_id)[0]["cash"]) if (user_credit - total_price) < 0: raise Exception("Insufficient Balance") #update users balance and add transaction to buy history table new_balance = user_credit - total_price db.execute( "UPDATE users SET cash = :new_balance WHERE id = :user_id", new_balance=new_balance, user_id=user_id) # check if user already has open positions for stock symbol, #if yes - increase amount else add new entry to open_positions table user_position = db.execute( "SELECT id, shares FROM open_positions WHERE symbol = :symbol AND user_id = :user_id", symbol=sym, user_id=user_id) if user_position: user_position = user_position[0] new_total = shares + user_position["shares"] update_position(db, new_total, price, user_position["id"]) else: db.execute( "INSERT INTO open_positions(id, user_id, symbol, price, shares) VALUES (NULL, :user_id, :symbol, :price, :shares)", user_id=user_id, symbol=sym, price=price, shares=shares) #create transaction object for template rendering transaction = { "type": "bought", "symbol": sym, "shares": shares, "price": price, "new_balance": usd(new_balance) } db.execute( "INSERT INTO transaction_history(id, user_id, symbol, price, shares, type) VALUES (NULL, :user_id, :symbol, :price, :shares, :_type)", user_id=user_id, symbol=sym, price=price, shares=shares, _type="buy") return render_template("success.html", transaction=transaction) except Exception as e: if str(e) in status_codes: #return exception name and status code if status code for given exception is in status_code dictionary defined at beginning of file return apology(str(e), status_codes[str(e)]) else: return apology(str(e))