使用while循环将值添加到字典

很酷的家伙

嘿,我的while循环在将值添加到字典中时遇到问题

contacts = {}
addContact = 'yes'
while addContact == 'yes':
    name1 = input('Please enter the name of the contact: ')
    num = int(input('Please enter the phone number of the contact: '))
    email1 = input('Please enter the email of the conact: ')

    contacts[name1] = num, email1

    addContact = input('Would you like to add another Contact? (yes or no): ')

    if addContact == 'no':
        break

print(contacts)

循环只会添加用户最后输入的值,如何获取所有值呢?

杰克逊·库顿(Jackson Coudron)

在Python 2.7中使用它并将输入更改为raw_input时,我收到以下输出:

C:\Python27\Doc\Python Programs\Book>test.py
Please enter the name of the contact: test1
Please enter the phone number of the contact: 123456789
Please enter the email of the conact: [email protected]
Would you like to add another Contact? (yes or no): yes
Please enter the name of the contact: test2
Please enter the phone number of the contact: 234567891
Please enter the email of the conact: [email protected]
Would you like to add another Contact? (yes or no): no
{'test1': (123456789, '[email protected]'), 'test2': (234567891, '[email protected]')}

使用您的确切代码。

您的代码运行良好。但是,如果您要输入多个值,请说一个相同的名称,号码或电子邮件。您将要更改值。例如,从我的输出中,如果我输入test1作为名称,其数字为123456789,然后输入另一个test1,其数字为987654321,它将用第二个替换第一个。您需要一组代码来检查是否有多个相同的输入,然后将它们输入为新联系人,而不是替换前一个联系人。

我会尝试添加添加功能的方法,该功能会进行扫描以查看字典联系人中是否已存在输入。如果确实如此,则将其添加到词典中的其他位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用for循环将键和值添加到空字典中

来自分类Dev

使用For循环混淆将表单添加到字典中

来自分类Dev

在“ while”循环下将值添加到标量列表

来自分类Dev

使用枚举键将值添加到Swift字典

来自分类Dev

使用枚举键将值添加到Swift字典

来自分类Dev

使用for循环将值添加到先前的值

来自分类Dev

将字典值添加到列表?

来自分类Dev

使用for循环将值添加到MySQL插入查询

来自分类Dev

Scala:使用for循环将值添加到sortedSet或数组

来自分类Dev

使用 for 循环将值添加到数组

来自分类Dev

将值附加到列表并添加到字典

来自分类Dev

如何通过循环将变量添加到字典中?

来自分类Dev

循环时将项目添加到Python字典

来自分类Dev

添加到字典的值

来自分类Dev

使用python中的循环将项目添加到现有字典中

来自分类Dev

使用python中的循环将项目添加到现有字典中

来自分类Dev

将值作为while循环生成的行添加到pd.dataframe

来自分类Dev

将字典添加到for循环内的列表中,每次运行的最后dict.key的值未正确添加

来自分类Dev

JavaScript-无法将字典值添加到数组

来自分类Dev

将属性值添加到字典中的对象

来自分类Dev

将值列表添加到字典列表

来自分类Dev

将具有值的键添加到字典

来自分类Dev

如何将多个值添加到字典键

来自分类Dev

如何将多个值添加到密钥字典

来自分类Dev

将字典的值添加到列表中

来自分类Dev

无法将nil值的字典添加到数组中

来自分类Dev

Python:将多个值添加到字典键

来自分类Dev

将多个值添加到字典键

来自分类Dev

将新键/值添加到字典