繁体   English   中英

Python中的字典和循环

[英]Dictionaries and Loops in Python

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

•在main中是一个查询用户输入“protocol”的循环

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

•如果T然后主要调用PT打印该值并继续..

•如果F然后主调用ADD,它提示输入值并将KV对添加到DB。

循环直到用户输入'end'然后打印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

首先,您正在使用input ,这主要是评估用户输入。 那么让我给你一个例子:

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

请改用raw_input 但是,如果您使用的是Python3,请继续使用input

你的问题在于TF 您实质上是在检查空字符串是否为空,因此对于任何类型的输入(非空),它将只打印出该值,因为即使输入是if语句, if语句也将评估为Truehello world东西。 更好的方法是这样的:

if user_input in DB

这将检查user_input是否在数据库字典的键中。

第三,当你for i in DB:写这个时,你正在遍历字典中的密钥对for i in DB: 你为什么要这样做呢? 只需使用in关键字搜索字典中的键,如上所述。 所以,一个正常运行的程序将是这样的:

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

这个TF(user_input) == True将永远为真。 因为DB是一个dictionary而且它总是!="" 它返回true并调用打印输入的PT(user_input) 所以从不调用ADD(user_input)

我认为你需要的是:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM