1

I have a system where a list of names are assigned to the value 0 using a dictionary. I would like to cause the value to increment by 1 whenever a user inputs their name.

I've tried using a for loop to iterate through the dictionary, however this only searches the dictionary for the name term by term and does not update the value if the inputted name is not in whatever iteration the loop is at.

i = 0
dic = {"tim":0, "jimbo":0}

while True:
    while i < len(dic):
        for name, value in dic.items():
            try:
                userinput = input("Enter name: ").lower()
            except(ValueError):
                print("Error")
            if userinput in name:
                dic[name] += 1
                i += 1
            print(dic)

The print statement at the end does give an updated value, but only if the entered name is within the current iteration of the loop. Ideally I would like the value to update whenever a name within the dictionary is inputted. The console also seems to freeze after four inputs.

Is there a more sensible way of doing this, and specifically doing this for a much larger dictionary with upwards of 100 keys?

b01111
  • 13
  • 2
  • 2
    What is the point of breaking the inner `while` loop? I am confused why you have that and also have the `for` loop. Also this is more a of question for codereview not SO. – Error - Syntactical Remorse Aug 07 '19 at 19:03
  • Can you clarify what your goal is here? How many times do you want to ask for user input? – Brendan A. Aug 07 '19 at 19:12
  • 1
    I'd like to continually ask for user input. So whenever the user inputs a name, and the value updates, user input is asked for again. This is going to be used to determine how many times a particular user has accessed the system and would really use email addresses instead of names. – b01111 Aug 07 '19 at 19:21

2 Answers2

1

To get the loop to continually ask for user input, just get rid of the inner while and for loops and change your test from if userinput in name to if userinput in dic.keys():

i = 0
dic = {"tim":0, "jimbo":0}

while True:
    try:
        userinput = input("Enter name: ").lower()
    except(ValueError):
        print("Error")
    if userinput in dic.keys():
        dic[userinput] += 1
    print(dic)

Of course you may want to build in an escape for yourself. The following will allow you to exit the infinite while with CTRL+c:

while True:
    try:
        try:
            userinput = input("Enter name: ").lower()
        except(ValueError):
            print("Error")
        if userinput in dic.keys():
            dic[userinput] += 1
        print(dic)
    except KeyboardInterrupt:
        break
Brendan A.
  • 1,268
  • 11
  • 16
  • Thanks, seems to work. Do you think this is the best way to go about this for a dictionary with say 100 keys? – b01111 Aug 07 '19 at 19:46
  • That depends on what your goal is. Dictionaries are very fast structures (see [this](https://stackoverflow.com/a/1419324/4325492) answer, for example) so efficiency is a concern, then it's going to be hard to do better. But without knowing more about the larger system you mention putting this in, there's no way to say if this is really the "best" option. – Brendan A. Aug 08 '19 at 14:18
0

Is this it ?

keys = dic.keys()
while True:
    key = input()
    if key in keys:
        dic[key] += 1
Pranav Void
  • 359
  • 3
  • 7