Python:跳回第一行

时间:2015-07-22 01:03:08

标签: python loops lines

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

到目前为止,我能够买入/卖出股票并通过循环重复这些订单,但我想回到第一线,我在询问是否要买入或卖出股票。我尝试用循环做但我没有真正成功。关于如何回到这条线的任何想法:

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

我知道我的很多代码仍然非常低效或愚蠢我只是想测试到目前为止我学到的东西而且在这篇文章中我真的只想知道回到

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

2 个答案:

答案 0 :(得分:1)

您需要将主菜单包含在另一个循环中。

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

答案 1 :(得分:0)

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

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