def game_loop(username): # Main menu (balance, buy, sell, lookup, quote, portfolio, exit) main_done = False while not main_done: balance_inputs = ["a", "balance"] buy_inputs = ["b", "buy"] sell_inputs = ["s", "sell"] lookup_inputs = ["l", "lookup"] quote_inputs = ["q", "quote"] portfolio_inputs = ["p", "portfolio"] exit_inputs = ["e", "exit"] acceptable_inputs = balance_inputs\ +buy_inputs\ +sell_inputs\ +lookup_inputs\ +quote_inputs\ +portfolio_inputs\ +exit_inputs user_input = view.main_menu(username) # If the user input is acceptable. if user_input.lower() in acceptable_inputs: # Balance if user_input.lower() in balance_inputs: balance = model.get_balance(username) view.balance_menu(balance) # Buy elif user_input.lower() in buy_inputs: ticker_symbol, trade_volume = view.buy_menu() res = model.buy(ticker_symbol, trade_volume, username) view.display_response(res) # Sell elif user_input.lower() in sell_inputs: ticker_symbol, trade_volume = view.sell_menu() res = model.sell(ticker_symbol, trade_volume, username) view.display_response(res) # Lookup elif user_input.lower() in lookup_inputs: company_name = view.lookup_menu() ticker_symbol = model.get_ticker_symbol(company_name) view.display_ticker_symbol(ticker_symbol) # Quote elif user_input.lower() in quote_inputs: ticker_symbol = view.quote_menu() last_price = model.get_last_price(ticker_symbol) view.display_last_price(last_price) # Portfolio (Holdings) elif user_input.lower() in portfolio_inputs: df = model.get_holdings_dataframe(username) balance = model.get_balance(username) earnings = model.get_earnings(username) view.display_dataframe(df, balance, earnings, username) # Exit elif user_input.lower() in exit_inputs: view.exit_message() main_done = True # Otherwise else: return "Error"
def game_loop(): user_input = view.main_menu() buy_inputs = ['b', 'buy'] sell_inputs = ['s', 'sell'] lookup_inputs = ['l', 'lookup'] quote_inputs = ['q', 'quote'] exit_inputs = ['e', 'exit'] view_inputs = ['v', 'view'] pl_inputs = ['p', 'p'] acceptable_inputs = buy_inputs \ + sell_inputs \ + lookup_inputs \ + quote_inputs \ + exit_inputs \ + view_inputs \ + pl_inputs on_off_switch = True while on_off_switch: if user_input.lower() in acceptable_inputs: if user_input.lower() in buy_inputs: (ticker_symbol, trade_volume) = view.buy_menu() x = model.buy(ticker_symbol, trade_volume) return x elif user_input.lower() in sell_inputs: (ticker_symbol, trade_volume) = view.sell_menu() x = model.sell(ticker_symbol, trade_volume) return x elif user_input.lower() in lookup_inputs: company_name = view.lookup_menu() x = model.lookup(company_name) return x elif user_input.lower() in quote_inputs: ticker_symbol = view.quote_menu() x = model.quote(ticker_symbol) return x elif user_input.lower() in view_inputs: balance = view.portfolio_menu() x = model.portfolio(balance) return x elif user_input.lower() in pl_inputs: p = view.pl_menu() x = model.pl(p) return x elif user_input.lower() in exit_inputs: break #on_off_switch = False else: print('Bad input. Restarting game in five seconds...') time.sleep(5) game_loop() else: return 'Plese Start Over'
def sell(username): (ticker_symbol, trade_volume) = view.sell_menu() order_status = model.sell(ticker_symbol, int(trade_volume), username) if order_status == 'SUCCESS': view.display_success() elif order_status == 'NO_FUNDS': holding_volume = model.get_holding_volume_by_symbol( username, ticker_symbol) view.display_insufficient_holdings(ticker_symbol, holding_volume)
def game_loop(): start = view.start_up_menu() if start == "s": (user_name, password) = view.sign_up_menu() model.sign_up(user_name, password) else: (user_name, password) = view.logg_in_menu() _id = model.user_check(password) print(_id) while 1: buy_inputs = ['b', 'buy'] sell_inputs = ['s', 'sell'] lookup_inputs = ['l', 'lookup', 'look up', 'look-up'] quote_inputs = ['q', 'quote'] exit_inputs = ['e', 'exit', 'quit'] view_inputs = ['v'] acceptable_inputs = buy_inputs \ +sell_inputs \ +lookup_inputs \ +quote_inputs \ +exit_inputs \ +view_inputs user_input = view.main_menu() if user_input in acceptable_inputs: if user_input in buy_inputs: balance = model.check_balance(user_name) fee = 6.25 ticker_symbol = view.quote_menu() price = model.quote(ticker_symbol) trade_volume = view.transaction_menu(balance, ticker_symbol, price) transaction_cost = (float(trade_volume) * price) + fee if transaction_cost <= balance: balance -= transaction_cost model.update_cash(user_name, balance) model.buy(user_name, ticker_symbol, float(trade_volume), float(price)) model.buy_holdings(user_name, ticker_symbol, float(trade_volume), price) else: print("Not enough monay") elif user_input in sell_inputs: ticker_symbol = view.quote_menu() price = model.quote(ticker_symbol) trade_volume = view.sell_menu() owned = model.check_holdings(user_name, ticker_symbol) if owned and int(trade_volume) <= owned: model.sell_holdings(user_name, ticker_symbol, trade_volume, price, owned) elif user_input in lookup_inputs: return model.lookup(view.lookup_menu()) elif user_input in quote_inputs: return model.quote(view.quote_menu()) elif user_input in view_inputs: users = model.get_users() users_list = [ ] # this is a list of all the users that currenttly have holdings for i in users: users_list.append(i[0]) users_holdings = [ ] # users holdings by tickers but not numbers for i in users_list: user_holdings = model.get_holdings(i) a = [] for i in user_holdings: a.append(i[0]) users_holdings.append(a) tickers = [ ] # is all the stocks the users, un "set'd" and joinined together for i in users_holdings: for j in i: tickers.append(j) prices = { } # this has all the prices for all the stocks owned in the holdings for i in set(tickers): # price = model.quote(i) prices[i] = price volumes = [ ] # a list of lists of amount of stock each of them own for i in users_list: volumes.append(model.holdings_volumes(i)) # print(users_holdings) # print(volumes) values_final = {} for i in range(len(users_holdings)): total = 0 for j in range(len(users_holdings[i])): total += (prices[users_holdings[i][j]]) * ( volumes[i][j][0]) values_final[users_list[i]] = total highest = 0 sorted_words = {} while values_final: for i in values_final: if values_final[i] >= highest: highest = values_final[i] word = i sorted_words[word] = highest values_final.pop(i) highest = 0 for i in sorted_words: print("{} : {}".format(i, sorted_words[i])) # highest = 0 # while values_final: # for i in range(len(values)) # if values[i] # need to add total balance #work vwamp elif user_input in exit_inputs: break
exit_terminal == True while pk == None: username, password = view.login_prompt(True) pk = model.login(username, password) exit_terminal = False while exit_terminal == False: option = view.main_menu() if option.strip() == "0": exit_terminal = True elif option.strip() == "1": balance = model.get_balance(pk) holdings = model.get_holdings(pk) view.show_status(balance, holdings) view.pause() elif option.strip() == '3': ticker_symbol, number_of_shares = view.sell_menu() status = model.sell(pk, ticker_symbol, number_of_shares) if status == False: view.sell_error() view.pause() else: view.sell_good() view.pause() elif option.strip() == '5': ticker_symbol = view.quote_prompt() quote = model.quote(ticker_symbol) view.display_quote(ticker_symbol, quote) view.pause() elif option.strip() == "2": account_pk = pk ticker_symbol = view.getsymbol()
def game_loop(): current_username = '' condition = True while 1: user_choice = view.log_or_sign() user_choice = user_choice.lower() log_in = ['l', 'login'] create_ = ['c', 'create'] exit_ = ['e', 'exit'] accept_input = log_in \ +create_ \ +exit_ if user_choice in accept_input: if user_choice in log_in: (user_name, password) = view.log_menu() current_username = user_name has_account = model.log_in(user_name, password) if has_account: break else: print('WRONG LOGIN INFORMATION. TRY AGAIN') import time time.sleep(3) elif user_choice in exit_: condition = False m.log_out() os.system('clear') break elif user_choice in create_: #(new_user,new_password,new_funds) = view.create_menu() # new_user = input("username:"******"password:"******"""INSERT INTO user( username, password, current_balance ) VALUES(?,?,? )""", newuser) connection.commit() cursor.close() connection.close() print("You have signed up!") import time time.sleep(3) while condition: buy_inputs = ['b', 'buy'] sell_inputs = ['s', 'sell'] lookup_inputs = ['l', 'lookup'] quote_inputs = ['q', 'quote'] exit_inputs = ['e', 'exit'] acceptable_inputs = buy_inputs \ +sell_inputs \ +lookup_inputs \ +quote_inputs \ +exit_inputs user_input = view.main_menu() if user_input in acceptable_inputs: if user_input in buy_inputs: (ticker_symbol, trade_volume) = view.buy_menu() confirmation_message, return_list = model.buy( current_username, ticker_symbol, trade_volume) if confirmation_message == True: yes = ['y', 'yes'] no = ['n', 'no'] choice = input( "You have enough money. Would you like to buy this stock?\n[y] Yes\n[n] No\n" ) if choice in yes: model.buy_db(return_list) else: print("Returning to main menu.") else: print("You do not have enough money to buy this stock.") elif user_input in sell_inputs: (ticker_symbol, trade_volume) = view.sell_menu() confirmation_message, return_list = model.sell( current_username, ticker_symbol, trade_volume) #TODO if confirmation_message == True: yes = ['y', 'yes'] no = ['n', 'no'] choice = input( "You have enough shares to sell. Would you like to sell this stock?\n[y] Yes\n[n] No\n" ) if choice.lower() in yes: model.sell_db(return_list) #TODO else: print("Returning to main menu.") else: print("You do not have enough shares to sell.") elif user_input in lookup_inputs: company_name = view.lookup_menu() print(model.lookup_ticker_symbol(company_name)) elif user_input in quote_inputs: #TODO ticker_symbol = view.quote_menu() print(model.quote_last_price(ticker_symbol)) #import time #time.sleep(5) elif user_input in exit_inputs: os.system('clear') break else: #catches huge error #should contain a fallback function pass else: pass model.updateHoldings() import time time.sleep(3)
def game_loop(): current_username = '' condition = True while 1: user_choice = view.log_or_sign() user_choice = user_choice.lower() log_in = ['l','login'] create_ = ['c','create'] exit_ = ['e','exit'] accept_input = log_in \ +create_ \ +exit_ if user_choice in accept_input: if user_choice in log_in: (user_name, password) = view.log_menu() current_username = user_name has_account = model.log_in(user_name, password) if has_account: break else: input("You have entered the wrong login information. \nPlease try again. Press enter to continue. ") elif user_choice in exit_: condition = False m.log_out() view.clear_screen() break elif user_choice in create_: model.create_() print("You have signed up!") input("Press enter to continue. ") while condition: #while true, the loop continues buy_inputs = ['b', 'buy'] sell_inputs = ['s', 'sell'] lookup_inputs = ['l', 'lookup'] quote_inputs = ['q', 'quote'] funds = ['f', 'funds'] holdings = ['h', 'holdings'] transactions = ['t', 'transactions'] exit_inputs = ['e', 'exit'] acceptable_inputs = buy_inputs \ +sell_inputs \ +lookup_inputs \ +quote_inputs \ +funds \ +holdings \ +transactions \ +exit_inputs user_input = view.main_menu() user_input = user_input.lower() if user_input in acceptable_inputs: if user_input in buy_inputs: (ticker_symbol, trade_volume) = view.buy_menu() confirmation_message, return_list = model.buy(current_username, ticker_symbol, trade_volume) if confirmation_message == True: #if the transaction cost is less than the current balance yes = ['y', 'yes'] no = ['n', 'no'] choice = input("You have enough money. Would you like to buy this stock?\n[y] Yes\n[n] No\n") if choice in yes: model.buy_db(return_list) print("You have bought {} shares of {}. ".format(trade_volume, ticker_symbol)) else: print("Returning to main menu.") else: print("You do not have enough money to buy this stock.") elif user_input in sell_inputs: (ticker_symbol, trade_volume) = view.sell_menu() confirmation_message, return_list = model.sell(current_username, ticker_symbol, trade_volume) if confirmation_message == True: yes = ['y', 'yes'] no = ['n', 'no'] choice = input("You have enough shares to sell. Would you like to sell this stock?\n[y] Yes\n[n] No\n") if choice.lower() in yes: model.sell_db(return_list) print("You have sold {} shares of {}. ".format(trade_volume, ticker_symbol)) else: print("Returning to main menu.") else: print("You do not have enough shares to sell.") elif user_input in lookup_inputs: company_name = view.lookup_menu() ticker_symbol = model.lookup_ticker_symbol(company_name) if len(ticker_symbol) <=6: #if it returns an actual ticker symbol instead of the error message print("The ticker symbol for {} is {}.".format(company_name, ticker_symbol)) else: #error message print("The ticker symbol for the company you searched cannot be found. \nPlease try again.") elif user_input in quote_inputs: ticker_symbol = view.quote_menu() last_price = model.quote_last_price(ticker_symbol) if len(str(last_price)) <= 6: print("The last price for {} is ${}.".format(ticker_symbol, last_price)) else: print("The last price for the company you searched cannot be found. \nPlease try again.") elif user_input in funds: view.clear_screen() bal = model.funds() print("Your current balance is ${}.".format(bal)) elif user_input in holdings: view.clear_screen() holdings = m.holdings() print("Your current holdings: \n{}".format(holdings)) elif user_input in transactions: view.clear_screen() transactions = m.transactions() print("All of your previous transactions: \n{}".format(transactions)) elif user_input in exit_inputs: view.clear_screen() condition = False m.log_out() break else: print("Error.") else: print("Error.") model.update_holdings() input("\nPress enter to continue. ")