0

My program is unable to go into the last of my if/else statement. My code:

def main_prog(): 
    while True:
        data_dict = {'123': 'viksun', '111': ['tord'], '333': ['test']} # Data storage.
        print (data_dict)           # Track-print
        prompt = input('Enter command >>> ').split(" ")
        if prompt[0] == 'lookup':   
            B_value = name_lookup_return_boolean(data_dict, prompt[1]) 
            if B_value == True:
                print (prompt[1],"'s number is:", name_lookup(data_dict, prompt[1])) 
            else:
                print("The name:" ,prompt[1], "do not exist, please try a different name")

Data struction of the dictionary: data_dict

data_dict = {'123': ['name1'], '111': ['tord'], '333': ['test']} # Data storage.

Function descriptions: - name_lookup_returns_boolean(data_dict, name) returns a boolean value: True if the name is located in the dictionary and False if the name does not exist. - name_lookup returns the key that corresponds to the specific name.

The functions have been tested separately from the whole program, so they are working. If you need them, I can also post them.

I can't get the program to run the last else statement in the program. This is going to run if the name does not exist in the dictionary: data_dict.

Here is the implementation of the two functions:

def name_lookup(data_dict, name):
    for key, value in data_dict.items():
        if name in value:
            return key



def name_lookup_return_boolean(data_dict, name):
    for value in data_dict.items():
        if name in value:
            return True
        else:
            return False

This is the variation I have tried to use_but with no sucess:

def name_lookup_version_02(data_dict, name):
   for value in data_dict.values():
       if name in value:
           return True
       else:
           return False
VIKSUN
  • 1
  • 2

2 Answers2

0

In this line:

if B_value == True:

I'm guessing that B_value holds a true value, but not True. Try this instead:

if B_value:
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
0

The problem is in your name_lookup_return_boolean function. You are returning True for both conditions. Also you should be enumerating both key and value otherwise value will be assigned a tuple.

It should look like this:

def name_lookup_return_boolean(data_dict, name):
  for key,value in data_dict.items():
    if name in value:
      return True

UPDATE: After testing, I realised previous answer was wrong - was only matching the first value

James Holderness
  • 22,721
  • 2
  • 40
  • 52
  • Thanks for your answer. But that didn't solve it. This are an example of a program run: {'111': ['tord'], '123': ['viksun'], '333': ['test']} Enter command >>> lookup asdd asdd 's number is: None {'111': ['tord'], '123': ['viksun'], '333': ['test']} Enter command >>> – VIKSUN May 08 '13 at 09:37
  • Did you try typing 'lookup 123' as I suggested in my answer. Also, as I said in my answer, you're going to need to provide the definitions of the name_lookup functions if you want more help, since that is likely where the problem is. – James Holderness May 08 '13 at 09:40
  • OK! Posting the 2 functions: – VIKSUN May 08 '13 at 11:08
  • def name_lookup(data_dict, name): for key, value in data_dict.items(): if name in value: return key def name_lookup_return_boolean(data_dict, name): for value in data_dict.items(): if name in value: return True else: return True And if I type: lookup 123 as you suggested the program print out: 123 's number is: None – VIKSUN May 08 '13 at 11:12
  • I've updated my answer with the explanation of what is wrong. – James Holderness May 08 '13 at 11:22
  • Note, that I've updated my answer again - the previous update wasn't entirely correct. – James Holderness May 08 '13 at 11:34
  • Yes! Do not work yet but think I can handle It now. If you guys think the two implementations of the functions is correct. James Holderness, but what about the unused variable in the function, name_lookup_return_boolean ? Got a warning from Aptana Studio that the variable |key| isn't in use.?? – VIKSUN May 08 '13 at 11:48
  • An alternative is to replace that line with `for value in data_dict.values():` – James Holderness May 08 '13 at 11:49
  • Hello! James Holderness. Are appreciate all the help you have giving me :) I finally solve the problem, thanks to your previously code. – VIKSUN May 08 '13 at 14:14