contacts = [ {"name": "Qi Lee", "mobile": 13946253589}, {"name": "Anny Hofman", "mobile": 13651323101} ] name = input() isFound = 2 分 for 2 分 : if 2 分 : isFound = True print(contact["mobile"]) break if isFound != True: print("用户不存在")
时间: 2023-04-05 07:05:02 浏览: 602
这是一个关于 Python 编程的问题,我可以回答。这段代码的作用是在联系人列表中查找指定姓名的联系人,并输出其手机号码。如果找到了该联系人,则将 isFound 设为 True,否则输出“用户不存在”。
相关问题
contacts = [ {"name": Qi Lee, "mobile": 13946253589}, {"name": Anny Hofman, "mobile": 13651323101} ] 输入示例1: Qi Lee 输出示例1: 13946253589 输入示例2: Yaping Guo 输出示例2: 用户不存在 contacts = [ {"name": "Qi Lee", "mobile": 13946253589}, {"name": "Anny Hofman", "mobile": 13651323101} ] name = input() isFound = for : if : isFound = True print(contact["mobile"]) break if isFound != True: print("用户不存在")
好的,根据你的要求,以下是 Python 代码实现:
```python
# 定义联系人列表
contacts = [
{"name": "Qi Lee", "mobile": 13946253589},
{"name": "Anny Hofman", "mobile": 13651323101}
]
# 获取用户输入的姓名
name = input()
# 查找联系人
isFound = False
for contact in contacts:
if contact["name"] == name:
isFound = True
print(contact["mobile"])
break
# 判断是否找到联系人
if isFound != True:
print("用户不存在")
```
以上代码中,我们首先定义了一个联系人列表 `contacts`,其中每个联系人都是一个字典,包含姓名和手机号两个键值对。接着,我们使用 `input()` 函数获取用户输入的姓名,然后使用 `for` 循环遍历联系人列表,查找是否存在该联系人。如果找到了该联系人,则输出其手机号,并将 `isFound` 标志变量设为 `True`;否则,输出“用户不存在”。
在如下通讯录中查找对应的联系人: contacts = [ {"name": Qi Lee, "mobile": 13946253589}, {"name": Anny Hofman, "mobile": 13651323101} ] 输入示例1: Qi Lee 输出示例1: 13946253589 输入示例2: Yaping Guo 输出示例2: 用户不存在 contacts = [ {"name": "Qi Lee", "mobile": 13946253589}, {"name": "Anny Hofman", "mobile": 13651323101} ] name = input() isFound = for : if : isFound = True print(contact["mobile"]) break if isFound != True: print("用户不存在")
好的,我可以为您编写一个查找联系人的程序。请使用以下代码:
```python
contacts = [
{"name": "Qi Lee", "mobile": 13946253589},
{"name": "Anny Hofman", "mobile": 13651323101}
]
name = input()
isFound = False
for contact in contacts:
if contact["name"] == name:
isFound = True
print(contact["mobile"])
break
if isFound != True:
print("用户不存在")
```
请按照要求输入联系人的姓名,程序将输出对应的手机号码。如果联系人不存在,则输出“用户不存在”。