首页 > 解决方案 > 在元组值中访问用户在 python 中输入的值的元组

问题描述

提前致谢!请建议正确的代码。我使用了以下代码,但无法访问用户输入的 empid。

我的查询是:

# Create Foll. Information in the Tuple (at least 5 Employees)
# 1. EmpId - Phone Numbers (One Employee can have Multiple Numbers )      
# 2. Accept Empid from User. Display his Numbers only if he exists in the Database(Tuple). Display App. Message if not present```

## My Code
empdetail = (("101",1234587),("102",2588467,2798478),("103",2689822,123456),("104",28398466),("105",8998666))
empid = input("Enter Employee Id :")


for name in empdetail:
    if empid in empdetail:
        res = empdetail[i][i:]
        print(str(res))
        break
    else:
        print("Employee not present.")
        break

print(empdetail)

标签: pythontuples

解决方案


我会将其转换为字典。

empdetail = (("101",1234587),("102",2588467,2798478),("103",2689822,123456),("104",28398466),("105",8998666))
empid = input("Enter Employee Id :")


emp_dict =  {key: values for key, *values in empdetail}

if empid in emp_dict.keys():
  print(*emp_dict[empid])

else:
  print("Employee not present.")

推荐阅读