如何使用Python中另一个字典中的键来更改列表中的字符?

2024-05-28 18:35:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个函数来检查ID的有效性。ID以a、B、C或D开头。我很难用字典中的键值替换用户输入中的A,B,C,Ds。~请帮帮我~

例如,如果用户输入A,则程序应检查A是否在字典中,如果在字典中,则程序将用键的值替换它,即10

谢谢

ID_dict = {"A": 10, "B" : 11, "C" : 12, "D" : 13}

ID = list(raw_input("What is your ID"))

def check_id(ID):

???

Tags: 函数用户程序idinputraw字典ds
2条回答

这就是你想要的:

ID = list(raw_input("What is your ID"))
def check_id(ID):
    for a in ID:
        if a.upper() in ID_dict:
            print a.upper() +" is there"
        else:
            print a.upper()+" is not there"

check_id(ID)

输出:

What is your IDasde
A is there
S is not there
D is there
E is not there
ID_dict = {"A": 10, "B" : 11, "C" : 12, "D" : 13}

ID = raw_input("What is your ID")

def check_id(ID):
     if ID in ID_dict:
          print "Yes Valid"

相关问题 更多 >

    热门问题