编写一个程序,首先接受由姓名和电话号码(都是字符串)组成的单词对,并用逗号分隔

问题描述 投票:0回答:6

我有以下提示:

联系人列表是您可以存储特定联系人以及其他相关信息(例如电话号码、电子邮件地址、生日等)的地方。编写一个程序,首先接受由姓名和电话号码组成的单词对(两个字符串),用逗号分隔。该列表后面是一个名称,您的程序应该输出与该名称关联的电话号码。假设搜索名称始终在列表中。

例如:

如果输入为:Joe,123-5432 Linda,983-4123 Frank,867-5309 Frank the 输出是:867-5309

我的代码:

pn = str(input()).split()

search = str(input())

i=0

for i in range(len(on)):

if pn[i] == (search):

print([i+1])

输入被分成姓名和号码。当代码检查名称是否相同时,它总是返回 false。我尝试过使用

re.split()
方法,但没有成功。

python list split
6个回答
0
投票

您应该拆分两次,第二个拆分字符应该是逗号

s.split(",")

s = "Joe,123-5432 Linda,983-4123 Frank,867-5309"

for i in s.split():
    temp = i.split(",");
    print("name :", temp[0] , " number is :" , temp[1] )

输出

name : Joe  number is : 123-5432
name : Linda  number is : 983-4123
name : Frank  number is : 867-5309

0
投票

搜索名称时尝试用逗号分隔:

pn = input().split()
search = input()

for i in pn:
  val = i.split(',')
  if val[0] == search:
    print(val[1])

0
投票

您的程序中需要具备以下内容才能满足基本要求

  1. 无限循环输入联系信息,直到用户停止输入
  2. 用于保存输入信息和搜索的列表或字典

代码可以像下面这样

contacts = {}

while True:
    info = input('Enter Contact Info or Leave empty to Stop Entering: ').split(',')
    if len(info) > 1:
        contacts[info[0]] = info[1]
    else:
        break

name = input('Enter name to search: ')
print(contacts[name])

输出如下


0
投票

您似乎正在尝试将数据存储为输入,向用户询问查询(人名),然后使用该人的电话号码进行响应。

# Get the data
inputs = input("Enter the input. >>> ").split(sep=" ")

# Split the data into lists
for pos in range(len(inputs)):
    inputs[pos] = inputs[pos].split(sep=",")

# Ask for a search query
query = input("Enter a name. >>> ")
# Check for the name in the first element of each item
for item in inputs:
    if item[0] == query:
        print(f"{query}'s phone number is {item[1]}.")
        break

示例数据输入,如第 2 行所示:

Enter the input. >>> John,12313123 Bob,8712731823

从代码的搜索查询行开始,您的

inputs
变量看起来像:
[['John', '12313123'], ['Bob', '8712731823']]
。程序将迭代
inputs
的项目,其中每个项目是两个字符串的列表,然后检查该子列表的第一项是否与输入的查询匹配。


0
投票
contact_list = input().split(sep=" ")
search_list = input()

def Convert(lst):
    res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
    return res_dct

contact_dict = Convert(contact_list)

print(contact_dict[search_list])

0
投票

我将用户输入转换为带有两个 for 循环和一个 zip() 方法的字典。然后我只是迭代字典找到键并打印出值。我对 Python 还很陌生,这花了我一段时间才弄清楚,但我知道这会对某人有所帮助。我确信还有很多其他更简单的方法来编写和解决这个问题,但这就是我解决它的方法。

userInput = input().replace(' ', ',') # places commas where white space is in string
correct = userInput.split(',') # seperates at the commas and creates a list with everything seperated
nameChoose = input().strip() # gets user input to search a name in the future dictionary

newList1 = [] # creating a new empty list to place names in
newList2 = [] # creating a new empty list to place numbers in
for i in correct:
    newList1 = correct[0::2] # starting with first name iterating to store each name in empty list

for i in correct:
    newList2 = correct[1::2] # starting with first number iterating to store each number in empty list

numDict = dict(zip(newList1, newList2)) # merges the two list together creating a dictionary

for k, v in numDict.items(): # iterates over keys (k) and values (v) over 
    if k == nameChoose:  
        print(v)  # prints the phone number only of the name the user choose
© www.soinside.com 2019 - 2024. All rights reserved.