Search code examples
pythonloopslines

Python: Jumping back to the first line


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?')

Solution

  • 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*