字典ptel={"王一":"86862398","张明":"86863211","李云":"86865743","周华":"86868921","吴天":"86865692"}中存放了用户的电话,现输入用户名,可以查询出对应的电话并输出。
时间: 2023-12-11 11:33:17 浏览: 95
以下是查询电话号码的Python代码:
```python
ptel = {"王一":"86862398","张明":"86863211","李云":"86865743","周华":"86868921","吴天":"86865692"}
str = input("请输入查找的姓名:")
print(ptel[str])
```
输入姓名后,程序会输出对应的电话号码。
--相关问题--:
1. 如何向字典中添加新的键值对?
2. 如何删除字典中的某个键值对?
3. 如何遍历字典并输出
相关问题
2、字典ptel={"王-":"86862398","张明":"86863211","李云":"86865743","周华":"86868921","吴天":"86865692"}中存放了用户的电话,
以下是两种遍历字典ptel的方法:
1.对字典的键key进行迭代:
```python
ptel={"王-":"86862398","张明":"86863211","李云":"86865743","周华":"86868921","吴天":"86865692"}
for k in ptel.keys():
print(k)
```
输出:
```
王-
张明
李云
周华
吴天
```
2.对字典的值value进行迭代:
```python
ptel={"王-":"86862398","张明":"86863211","李云":"86865743","周华":"86868921","吴天":"86865692"}
for v in ptel.values():
print(v)
```
输出:
```
86862398
86863211
86865743
86868921
86865692
```
阅读全文