python - 在元组值中访问用户在 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)
解决方案
我会将其转换为字典。
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.")
推荐阅读
- google-cloud-sql - Google Cloud SQL 上的 PostgREST:unix 套接字 URI 格式?
- flutter - 转换为类型类 dart http 响应体
- java - 依赖关系引入了一个传递库,该库使用 TIFF 文件破坏 ImageIO.read
- java - 如何从处理器内的 Apache Camel 中的路由获取响应并在处理器内调用该路由?
- groovy - Spock-在被测类的私有方法调用中捕获方法参数
- scala - Scala Style:所有分支的模式匹配都是带有模式保护的通配符
- node.js - 使用特定选项或环境变量在 AWS lambda 中启动 NodeJS 运行时
- apache-spark - Apache Spark 驱动程序日志未指定阶段取消的原因
- docker - docker中的Gulp watch在docker中花费太长时间
- .net-core - 实体框架核心 3.1 中的 Pluralizer 模型名称