Question

Write a program that centers around a dictionary called DB that contains protocol names as keys and text descriptions of those protocols as values. No global variables.

• In main is a loop that queries user to enter “protocol”

• Loop calls TF to see if the protocol (key) exists in DB. TF returns a T or F.

• If T then main calls PT which prints the value and continues..

• If F then main calls ADD which prompts for a value and adds the KV pair to the DB.

Loop until user enters ‘end’ then print the DB

This is what I have so far:

#!/usr/bin/python3

#contains protocol names as keys, and text descriptions of protocols as values
DB= {'ICMP': 'internet control message protocol', 'RIP':'RIP Description',
     'ipv4':'Internet protocol v4', 'ipv6':'IP version 6'}

def TF(x):
    return x in DB

def PT(x):
    print("The protocol you entered is: " , x)
    return x

def ADD(x):
    usr_input = input("Please enter a value to be added to the DB: ")
    description = input("Please enter description for the key: ")
    DB[usr_input] = description


for i in DB:
    user_input = input("Please enter the protocol: ")
    if (user_input == "end"):
        break
    TF(user_input)
    if (TF(user_input) == True):
        PT(user_input)
    else:
        ADD(user_input)

I am getting the prompt for user input, but whenever I do something like enter "ICMP" at the prompt, it just prints out the same answer and continues looping infinitely until I hit Control+D. What am I doing wrong here? Even if I enter a key not in the dictionary, it does the same thing. Please help. Thanks.

EDIT: Fixed the infinite loop problem and edited PT(x) to show that it is being called. Now also fixed the problem so that ADD(x) is now called when there is no key value present in DB.

Persisting problem: Even if I enter, for instance "ICMP" as the input, it returns only the key itself, and the not the value associated with the key? How do I make the value appear?

Secondly, now that ADD(x) is being called if the user input isn't already present, however, it isn't appending to the DB dictionary and printing it out. Instead I get the following:

Please enter the protocol: ICMP
The protocol you entered is:  ICMP
Please enter the protocol: icmp
Please enter a value to be added to the DB: here here
Please enter description for the key: herererere
Traceback (most recent call last):
  File "D:/Sheridan/Part Time/Linux Architecture w. Network Scripting/Week 8 Code/practise_in_class.py", line 24, in <module>
    for i in DB:
RuntimeError: dictionary changed size during iteration
Was it helpful?

Solution

Firstly, you are using input, this is essentially evaluating the user input. So let me show you with an example:

>>> input("?")
?>? 1 + 1
2

Use raw_input instead. If you are using Python3 however, keep using input.

Your problem lies in TF. You are essentially checking whether the empty string is empty or not, so for any kind of input (that is not empty), it will will simply print out the value, because it will the if statement will evaluate to True even if the input is something like hello world. A better approach would be something like this:

if user_input in DB

This will check to see if user_input is in the keys for your database dictionary.

Thirdly, you are looping through the keys pairs in your dictionary, when you write this for i in DB:. Why are you doing this in the first place? Simply use the in keyword to search for keys in a dictionary as described above. So, a functioning program would be something like this:

DB = {'ICMP': 'internet control message protocol', 'RIP': 'RIP Description',
      'ipv4': 'Internet protocol v4', 'ipv6': 'IP version 6'}


if __name__ == '__main__':
    # Running loop
    while True:
        # Taking user input
        user_input = raw_input("Please enter a protocol, press Q to quit")

        # If the input is Q, then we break the loop
        if user_input == 'Q':
            break

        # If the value of user_input is inside DB, then we print it
        if user_input in DB:
            print DB[user_input]
        # Else, we ask the user to add a description, and add it to our dictionary
        else:
            user_value = raw_input("Please enter a value for your new protocol")
            # Adding to dictionary using the update method
            DB.update({user_input: user_value})

OTHER TIPS

this TF(user_input) == True will always be true. because DB is a dictionary and its always !="". it returns true and calls PT(user_input) which prints the input. So ADD(user_input) is never called.

i think what you need is:

def TF(x):
    return x in DB

so if it exists it returns true else false to insert it

>>> DB= {'ICMP': 'internet control message protocol', 'RIP':'RIP Description',
...      'ipv4':'Internet protocol v4', 'ipv6':'IP version 6'}
>>> "ICMP" in DB
True
>>> "ICMP-false" in DB
False
>>> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top