Python中的字典和循环

Python中的字典和循环,python,for-loop,dictionary,main,Python,For Loop,Dictionary,Main,编写一个以称为DB的字典为中心的程序,该字典将协议名称作为键,并将这些协议的文本描述作为值。没有全局变量 •在main中有一个循环,查询用户输入“协议” •循环调用TF,查看DB中是否存在协议(密钥)。TF返回一个T或F •如果T,则main调用PT,PT打印值并继续 •如果为F,则主调用ADD,提示输入值,并将KV对添加到DB中 循环,直到用户输入“结束”,然后打印数据库 这就是我到目前为止所做的: #!/usr/bin/python3 #contains protocol names as

编写一个以称为DB的字典为中心的程序,该字典将协议名称作为键,并将这些协议的文本描述作为值。没有全局变量

•在main中有一个循环,查询用户输入“协议”

•循环调用TF,查看DB中是否存在协议(密钥)。TF返回一个T或F

•如果T,则main调用PT,PT打印值并继续

•如果为F,则主调用ADD,提示输入值,并将KV对添加到DB中

循环,直到用户输入“结束”,然后打印数据库

这就是我到目前为止所做的:

#!/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)
我得到了用户输入的提示,但每当我在提示下输入“ICMP”时,它只会打印出相同的答案并继续无限循环,直到我点击Control+D。我在这里做错了什么?即使我输入一个不在字典中的键,它也会做同样的事情。请帮忙。谢谢

编辑:修复了无限循环问题,并编辑了PT(x)以显示正在调用它。现在还修复了这个问题,因此现在在DB中没有键值时调用ADD(x)

持久性问题:即使我输入,例如“ICMP”作为输入,它也只返回键本身,而不返回与键关联的值?如何显示该值

其次,如果用户输入不存在,那么现在调用ADD(x),但是,它不会附加到DB字典并将其打印出来。相反,我得到了以下结果:

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
请输入协议:ICMP
您输入的协议是:ICMP
请输入协议:icmp
请在此处输入要添加到DB的值
请输入密钥的说明:herere
回溯(最近一次呼叫最后一次):
文件“D:/Sheridan/Part-Time/Linux-Architecture w.Network Scripting/Week 8 Code/practice_in_class.py”,第24行,in
对于以DB表示的i:
RuntimeError:字典在迭代期间更改了大小
TF(用户输入)=True
将始终为True。因为
DB
是一个
字典
,它总是
=“”
。它返回true并调用打印输入的
PT(用户输入)
。因此,永远不会调用
ADD(用户输入)

我想你需要的是:

def TF(x):
    return x in DB
因此,如果它存在,则返回true,否则返回false以插入它

>>> 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
>>> 

首先,您使用的是
输入
,这实质上是评估用户输入。让我举个例子给大家看:

>>> input("?")
?>? 1 + 1
2
使用
原始输入
。但是,如果您正在使用Python3,请继续使用
input

您的问题在于
TF
。您实际上是在检查空字符串是否为空,因此对于任何类型的输入(不是空的),它都将简单地打印出值,因为
if
语句将计算为
True
,即使输入类似于
hello world
。更好的方法是这样的:

if user_input in DB
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})
这将检查
user\u input
是否在数据库字典的键中

第三,当您为DB中的i编写此
时,您正在遍历字典中的键对:
。你为什么要这么做?如上所述,只需在关键字中使用
,即可在字典中搜索关键字。因此,一个有效的程序应该是这样的:

if user_input in DB
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})

那么,我如何使其能够调用ADD(user_input)并将输入附加到DB并打印出来呢?TF此时将始终返回true,如果您将其更改为我在答案中添加的内容,那么如果它不在DB中,它将返回false,因此它将被添加。请注意,您可能不需要单独的功能,因为@Games Brainiac演示了Alright,我修复了这一点。请查看我的最新回复。提前谢谢!你还忘了提到他在DB上循环,在一个for循环中,这是适得其反的。@GamesBrainiac是的,你是对的。没有看到,虽然真的应该如此,但Python3中没有原始输入,您没有指定。@user1819786如果这个答案对您有帮助,请向上投票并接受它。•主要是一个循环,询问用户输入“协议”。你的“主要”街区在哪里?您需要在
中写入代码的使用输入部分,如果
块中的
。而且,您需要运行无限循环,而不是在DB上运行循环。