Python输出: "Not Found"

标签 python

使用以下代码: 我需要添加什么来添加以下场景: 输入:vlookup(“001-999999-999”, user_list, 3) 输出:“未找到”

def finder(lookupvalue,lookuplist,col_index):
    user_list = [
        ["Account number","Currency","Balance"],
        ["001-987654-003","USD",1300.72],
        ["001-919202-100","EUR",105.01],
        ["001-905700-100","EUR",305.00],
        ["001-908415-307","CHF",20804.98],
        ["011-974777-200","PLN",208.15],
        ["001-931654-001","USD",450.7]
    ]

    column=len(user_list[0])
    errormsg='Out of range'

    if col_index>3:
        raise ValueError(errormsg)
    
    for row in user_list: #for every row in the list
        if row[0]==lookupvalue: #if index 0 of n row is equal to lookupvalue
            print(row[col_index-1]) #look up  and print based on the index number. Less 1 will translate to Python language

最佳答案

你可以尝试这样的事情:

user_list = [
        ["Account number","Currency","Balance"],
        ["001-987654-003","USD",1300.72],
        ["001-919202-100","EUR",105.01],
        ["001-905700-100","EUR",305.00],
        ["001-908415-307","CHF",20804.98],
        ["011-974777-200","PLN",208.15],
        ["001-931654-001","USD",450.7]
    ]


    
def finder(lookupvalue,lookuplist,col_index):
    column=len(user_list[0])
    errormsg='Out of range'

    if col_index>3:
        raise ValueError(errormsg)

    count = 0
    for row in user_list: #for every row in the list
        if row[0]==lookupvalue: #if index 0 of n row is equal to lookupvalue
            print(row[col_index-1]) #look up  and print based on the index number. Less 1 will translate to Python language
            count += 1

    if not count:
        print("Not found")
finder('001-999999-999', user_list, 3)

输出:

Not found

关于Python输出: "Not Found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69068278/

相关文章:

找不到元素时 Python Selenium 崩溃

python - 检查子进程调用是否成功执行

python - openCV:如何使用 getPerspectiveTransform

Python 子进程 : in which case a segfault program returns -11 or 139?

python - 将 TensorFlow 低级 API(张量/占位符)与 Keras 模型相结合

python - pyqt4中的线程

python - 当 'b' 是我的类(class)时,覆盖 a-b 的 __sub__

python - 安装 pip : python-pip? 或从 python-setuptools

python - 在 Google Colab 中加载某些 NLTK 模块时出错

python numpy (v1.15.0) 无法拟合抛物线