Python:跳回第一行

Python: Jumping back to the first line

我刚开始学习 python 并想尝试编写一个非常基础的股票游戏。

到目前为止,我可以 buy/sell 股票并通过一个循环重复这些订单,但我想回到第一行,在那里我询问我是想买还是卖股票。我试着用一个循环来做,但我并没有真正成功。关于如何回到这条线的任何想法:

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

我目前拥有的:

# 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]')

我知道我的很多代码仍然非常低效或愚蠢我只是想测试我到目前为止学到的东西,在这个 post 我真的只想知道如何返回至

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

您需要将主菜单包含到进一步的循环中。

while True:
     order = input('Do you want to buy or sell? ')
     while backtooptions == 'n':
          *logic here*

您可以创建另一个 while 循环并在跳过其余代码后使用 continue 语句重复。例如:

backtooptions = input('Do you want to go back to the menu?[y/n]')
if backtooptions == "y":
    continue;