调用正确的函数

2024-04-24 09:12:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

def pStockName():

        global StockList,fStockList
        fStockList = []
        pStockList = []
        StockList = str(raw_input('Enter pipe separated list of StockS : ')).upper().strip()
        items = StockList.split("|")
        count = len(items)
        print  'Total Distint Stock Count : ',  count
        items = list(set(StockList.split("|")))
        pipelst = [i.replace('-mc','').replace('-MC','').replace('$','').replace('^','') for i in items]
        filepath = '/fsnadmin/SAAS_SUPPORT/Stock_data.txt'
        f = open(filepath, 'r')
        for lns in f:
                split_pipe = lns.split(':', 1)
                if split_pipe[0] in pipelst:
                        index = pipelst.index(split_pipe[0])
                        pStockList=split_pipe[0]+"|"
                        fStockList.append(pStockList)
                        del pipelst[index]

        for lns in pipelst:
                print bcolors.red + lns,' is not found in SaaS Stock Inventory' + bcolors.ENDC
         if pipelst:
        #if not lns:
                uResp = str(raw_input('Do You Want To Continue with option 0 [YES|Y|NO|N] : ')).upper().strip()
                if uResp == "YES" or uResp == "Y":
                        pStockName ()
                else:
                        #StockList = None
                        print_menu ()
        f.close()

当我执行此代码时,它会提示我编写股票列表,并提示我是否给出了所有正确的股票(在/location/stock中提到)_数据.txt,调用上述代码)正确执行如下:

Enter pipe separated list of StockS : aaa|hfa|fff

Total Distint Stock Count : 3

Stocks Belonging to other Centers :

Stock Count From Other = 0

Stocks Belonging to Current Centers :

Active Stocks in US1:

^AAA$|^HFA$

Terminated Stocks in US2:

^FFF$

Ignored Stock Count From Current Center = 1

You Have Entered StockList belonging to this center as: ^AAA$|^HFA$

Active Stock Count : 2

Do You Want To Continue [YES|Y|NO|N] :

如果给错了所有的存货(如果没有在/地点/存货_数据.txt并执行上述代码,得到如下结果:

Enter your choice [0-26] : 0

Enter pipe separated list of StockS : jjj|uuu|oo

Total Distint Stock Count : 3

UUU is not found in Stock list

OO is not found in Stock

JJJ is not found in Stock

Do You Want To Continue with option 0 [YES|Y|NO|N] : Y

Enter pipe separated list of stocks :

到目前为止,我得到了我需要的结果。但是,如果我在正确的股票之间给出了错误的股票,则不再要求输入单独的股票列表,尽管它的执行方式如下:

Enter pipe separated list of StockS : aaa|hfa|ooo

Total Distint Stock Count : 3

OOO is not found in Stock list

Stocks Belonging to other Data Centers :

Stock Count From Other centers = 0

Stocks Belonging to Current Data Centers :

Active Stocks in US1 :

^AAA$|^HFA$

Ignored Stock Count From Current center = 0

You Have Entered StockList belonging to this center as: ^AAA$|^HFA$

Active Stock Count : 2

Do You Want To Continue [YES|Y|NO|N] :

正如您在上面的语句中看到的,在使用正确的股票执行代码之后,我得到了错误的股票信息(在股票列表中找不到OOO)。 我想要的是,如果我给出了错误的股票,它不应该在给出错误的股票信息之后继续,然后它应该在给出选项Y(yes)之后再次要求输入一个单独的股票列表,就像我在给出所有错误的股票时得到的声明一样。你知道吗

请让我知道如何才能做到这一点。你知道吗

请看下表

Enter your choice [0-26] : 0

Enter pipe separated list of StockS : aaa|hfa|kk

Total Distint Stock Count : 3

KK is not found in Stock Inventory

Do You Want To Continue with option 0 [YES|Y|NO|N] : Y

Enter pipe separated list of StockS : aaa|hfa

Total Distint Stock Count : 2

Stocks Belonging to other Centers :

Stock Count From Other center = 0

Stocks Belonging to Current Centers :

Active Stocks in US1 :

^AAA$|^HFA$

Ignored Stock Count From Current center = 0

You Have Entered StockList belonging to this center as: ^AAA$|^HFA$

Active Stock Count : 2

Do You Want To Continue [YES|Y|NO|N] : Y

Stocks Belonging to other Centers :

Stock Count From Other center = 0

Stocks Belonging to Current Centers :

Active Stocks in US1 :

^AAA$|^HFA$

Ignored Stock Count From Current center = 0

You Have Entered StockList belonging to this center as: ^AAA$|^HFA$

Active Stock Count : 2

Do You Want To Continue [YES|Y|NO|N] :


Tags: toinyoucountstocklistyes股票
1条回答
网友
1楼 · 发布于 2024-04-24 09:12:45

似乎你想要:

if pipelst: #if pipelst is not empty

而不是

if not lns:

第一个将检查pipelst是否有元素,而第二个将执行其他操作。(I认为它将检查上一条for each语句中迭代的最后一个元素是否为0/空,但不确定这一点。)

当单个股票无效时,如果您想输入这个if,并且此时pipelst似乎包含所有无效股票,那么检查pipelst是否为空似乎是一个不错的选择。你知道吗

相关问题 更多 >