0

Hey Im having a problem with my while loop for adding values to dictionary

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)

The loop will only add the values that were last input by user, how can I get it to add all values?

5
  • I ran your code and it works just fine. By the way, you do not need the if addContact == 'no' block.
    – Hai Vu
    Dec 5, 2015 at 2:59
  • Your code seems like works fine, what's the problem? What do you mean about the only add the values that were last input by user ?
    – Remi Guan
    Dec 5, 2015 at 3:03
  • Are you entering multiple people with the same name? Dec 5, 2015 at 3:03
  • Yes that was the problem I entered the same name haha thanks
    – coolguy
    Dec 5, 2015 at 3:58
  • 1
    Ok, so do you want to figure out how to avoid that problem in the first place, or do you want to just end it here. If you don't want to go any further then please unaccept the answer so the question can be automatically deleted. Alternatively you can unaccept the answer and delete the question yourself using the delete link below your post.
    – user4639281
    Dec 5, 2015 at 4:02

1 Answer 1

-1

While using this in Python 2.7 and changing the inputs to raw_input I received the following output:

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]')}

Using your exact code.

Your code is running fine. However if you are entering multiple values let's say a same name, number, or email. You are going to change the value. For example from my output, if I put in test1 as the name with the number 123456789 then entered another test1 with the number 987654321 it will replace the first one with the second one. You need to have a set of code to check for multiple inputs that are the same and then inputs them as a new contact rather than replacing the previous one.

I would try something along the lines of adding a function that scans to see if the input already exists in the dictionary contacts. If it does then add it to the dictionary in a different place.

2
  • 1
    This answer does identify a potential problem and a potential solution, but does not actually offer a solution to the problem. Please edit your answer to provide the solution to the problem you've identified in order to turn this into a good answer. Otherwise, it is little more than an extended comment, which is not what the answer section is for.
    – user4639281
    Dec 5, 2015 at 3:59
  • Yes that was my problem I input multiple values with the same name. thanks for the help
    – coolguy
    Dec 5, 2015 at 3:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.