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