Python for循环重复输出input(),用于字典contact_email的每次迭代(不是我想要的)

6tqwzwtp  于 2022-10-30  发布在  Python
关注(0)|答案(8)|浏览(213)
contact_emails = {

         'Sue Reyn' : 's.reyn@email.com',
         'Mike Filt': 'mike.filt@bmail.com',
         'Nate Arty': 'narty042@nmail.com'
             }

         new_contact = input()

         new_email = input()
         contact_emails[new_contact] = new_email

         for contact in contact_emails:
                      print(new_contact + ' is ' + new_email)

我想要的输出是

s.reyn@email.com is Sue Reyn
mike.filt@bmail.com is Mike Filt
narty042@nmail.com is Nate Arty
alf1@hmail.com is Alf

输出我得到它

Alf is alf1@hmail.com
Alf is alf1@hmail.com
Alf is alf1@hmail.com
Alf is alf1@hmail.com

我做错了什么?我应该如何修复这段代码,使它输出我希望它输出的东西?

yhxst69z

yhxst69z1#

for contact in contact_emails:
    print(new_contact + ' is ' + new_email)

您一次又一次地打印新的联系人。您需要做的事情如下所示:

for contact, email in contact_emails.items():
    print(email + ' is ' + contact)

这将迭代字典中的所有键、值对。

lyr7nygr

lyr7nygr2#

而不是使用这个

contact_emails[new_contact] = new_email

试试这个

contact_emails.update({new_contact:new_email})

for key,val in contact_emails.items():
    print(key +' is ' + val)
3phpmpom

3phpmpom3#

你在contact_emails中遍历每个contact,但是变量contact没有出现在print调用中,你可以使用contact中的名字访问电子邮件:

...
for contact in contact_email:
    print(contact_email[contact], 'is', contact)

请记住,new_emailnew_contact是在循环的 * 外部 * 赋值的,因此在每次迭代时都不会更改。

csbfibhn

csbfibhn4#

在此处了解有关迭代字典的更多信息-How to Iterate Through a Dictionary in Python

contact_emails = {
    'Sue Reyn' : 's.reyn@email.com',
    'Mike Filt': 'mike.filt@bmail.com',
    'Nate Arty': 'narty042@nmail.com'
    }

new_contact = input()

new_email = input()
contact_emails[new_contact] = new_email

for contact in contact_emails:
            print(contact_emails[contact]+ ' is ' + contact)

输出量:

>>Alf
>>alf1@hmail.com
s.reyn@email.com is Sue Reyn
mike.filt@bmail.com is Mike Filt
narty042@nmail.com is Nate Arty
alf1@hmail.com is Alf
yrefmtwq

yrefmtwq5#

这就是我的小脑袋最后的工作。

for contact, email in contact_emails.items():
    print('{} is {}'.format(email, contact))
wgeznvg7

wgeznvg76#

带输入的示例输出:“Alf”“alf1@hmail.com”是这样一个:

mike.filt@bmail.com is Mike Filt
s.reyn@email.com is Sue Reyn
narty042@nmail.com is Nate Arty
alf1@hmail.com is Alf

字典contact_emails的大小已经是3,并且包含了三个联系人的详细信息。当添加新的联系人和电子邮件地址时,问题会要求显示所有四个联系人及其电子邮件ID。下面是如何完成的:

contact_emails = {
    'Sue Reyn' : 's.reyn@email.com',
    'Mike Filt': 'mike.filt@bmail.com',
    'Nate Arty': 'narty042@nmail.com'
}

new_contact = input()
new_email = input()
contact_emails[new_contact] = new_email

for key,val in contact_emails.items():
    print(f'{val} is {key}')

现在的输出为:

s.reyn@email.com is Sue Reyn
mike.filt@bmail.com is Mike Filt
narty042@nmail.com is Nate Arty
knguyen@qmail.com is Kate Nguyen

希望这对你有帮助!

eivnm1vs

eivnm1vs7#

contact_emails = {
    'Sue Reyn' : 's.reyn@email.com',
    'Mike Filt': 'mike.filt@bmail.com',
    'Nate Arty': 'narty042@nmail.com'
}

new_contact = input()
new_email = input()
contact_emails[new_contact] = new_email

for new_contact in contact_emails:
    print(contact_emails[new_contact] + ' is ' + new_contact)
z8dt9xmd

z8dt9xmd8#

对于联系人电子邮件中的新联系人:print(f '{联系人电子邮件[新联系人]} is {新联系人}')

相关问题