0

I just started learning python and wanted to try coding a VERY basic stock game.

So far I am able to buy/sell stocks and repeat these orders through a loop but I want to get back to the first line, where I am asking whether I want to buy or sell stocks. I tried doing it with a loop but I didn't really succeed. Any ideas on how i can get back to this line:

order = input('Do you want to buy or sell?')

What I have so far:

# Ordering and selling Stocks.
depot = {'aapl': [20, 400.0]} # amount, volume
cash = 20*20
order_book = {} #Stock, Price, Amount, Ordertype, Volume --> Key = timestamp?

#start menu: Buying or selling?

order = input('Do you want to buy or sell? ')
backtooptions = 'n'
while backtooptions == 'n':
    #buying stocks
    if order == "Buy" or order == 'buy':
        dstock = str(input('What stock do you want to buy? Enter ID!'))
        #buying stock if stock is in depot
        if dstock in depot:
            dprice = float(input('What price?'))
            damount = int(input('How many?'))
            volume = dprice*damount
            if cash >= volume:
                order_book[dstock] = dstock, dprice, damount, 'Buy', volume
                depot[dstock][0] += damount
                depot[dstock][1] += volume
                cash -= volume
                print('You just bought',order_book[dstock][2],'stocks of',
                      order_book[dstock][0],'worth', order_book[dstock][4],'€', 'at a price of', order_book[dstock][1],'€.')
                print (depot)
                print (cash)
                backtooptions = input('Do you want to go back to the menu?[y/n]')

            else:
                print('You do not have enough money!')
                backtooptions = input('Do you want to go back to the menu [y] or change your order[n]?')
        #buying stocks if stock is not in depot
        else:
            dprice = float(input('What price?'))
            damount = int(input('How many?'))
            volume = dprice*damount
            if cash >= volume:
                depot[dstock] = [damount, dprice, volume]
                order_book[dstock] = [dstock, dprice, damount, 'Buy', volume]
                cash -= volume
                print('You just bought',order_book[dstock][2],'stocks of',order_book[dstock][0],'worth', order_book[dstock][4],'€', 'at a price of', order_book[dstock][1],'€.')
                print (depot)
                print (cash)
                backtooptions = input('Do you want to go back to the menu?[y/n]')

            else:
                print('You do not have enough money!')
                backtooptions = input('Do you want to go back to the menu [y] or change your order[n]?')




    #selling stocks
    elif order == 'Sell' or order == 'sell':
        dstock = str(input('What stock do you want to sell? Enter ID!'))
        dprice = float(input('What price?'))
        damount = int(input('How many?'))
        #Do we have enough stocks?
        if damount <= depot[dstock][0]:#
            volume = damount*dprice
            order_book[dstock] = [dstock, dprice, damount, 'Sold', volume]
            depot[dstock][0] -= damount
            depot[dstock][1] -= volume
            cash += volume
            print('You just sold',order_book[dstock][2],'stocks of',order_book[dstock][0],'worth', order_book[dstock][4],'€', 'at a price of', order_book[dstock][1],'€.')
            print (depot)
            print (cash)
            backtooptions = input('Do you want to go back to the menu?[y/n]')
            volume = dprice*damount


        else:
            print('You do not have enough stocks to sell!')
            backtooptions = input('Do you want to go back to the menu [y] or change your order[n]?')

    else:
        print('Error!')
        backtooptions = input('Do you want to go back to the menu?[y/n]')

I am aware that a lot of my code is still very inefficient or stupid I am just trying to test what I've learnt so far and in this post I really only want to know about getting back to

order = input('Do you want to buy or sell?')
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
alpenmilch411
  • 483
  • 1
  • 5
  • 18
  • related (not duplicate, but kind of interesting): http://stackoverflow.com/questions/438844/is-there-a-label-goto-in-python – NightShadeQueen Jul 22 '15 at 01:06
  • Have you tried clearing screen and reprint options ? You can use os.system('clear') # on linux / os x – gogasca Jul 22 '15 at 01:06

2 Answers2

1

You need to include your main menu into a further loop.

while True:
     order = input('Do you want to buy or sell? ')
     while backtooptions == 'n':
          *logic here*
0x90
  • 6,079
  • 2
  • 36
  • 55
0

You can create another while loop and use the continue statement to reiterate after skipping the rest of the code. For example:

backtooptions = input('Do you want to go back to the menu?[y/n]')
if backtooptions == "y":
    continue;
deezy
  • 1,480
  • 1
  • 11
  • 22
  • Ah, I may have misunderstood your question. However, using `continue` will still be helpful when you begin working on this part. – deezy Jul 22 '15 at 01:23