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 - AWS IoT Python SDK 和 asyncio

python - os.walk() 是否缺少指向目录的符号链接(symbolic link)?

python - 无法使用 minidom 保存 xml 文件

python - 类似于 MATLAB 的 python 切片

python - 如何在编辑后验证 pydantic 对象

python - Python 中的 JSON 导航 (PowerBI API)

python - 提取括号内的字符串 - PYTHON

python - 如何从 IDE 使用 Vpython(视觉)

python - tf.gradients 和 tf.train.RMSPropOptimizer(lr_rate).compute_gradients 有什么区别?

python - OAuth 的 GAE 导入失败