【问题标题】:How to use loop when data type is dictionary数据类型为字典时如何使用循环
【发布时间】:2025-12-20 06:15:15
【问题描述】:
suspect = {'Height':6,'Colour':'Fair','Residency':'Pune'}
Name = input('Please enter your name')
Verification = int(input('Please enter Height ')

if Verification == suspect['Height']:
    print('Suspect is detected  for 1st level')
else:
    print('Suspect is not detected  for 1st level')**

请注意,我的情况是:如果使用{'Height': 3, 'Colour': 'Fair', 'resident': 'Pune'}-condition 检测到suspect,则应保存名称并继续运行代码。意思是'Please enter your name''Please Enter Height'应该继续问。

【问题讨论】:

  • 如果没有检测到嫌疑人怎么办?它应该继续运行你的代码吗?

标签: python-3.x dictionary if-statement


【解决方案1】:

您需要使用 while 循环。在每个循环中,程序都会询问您是否要继续。 此外,列表允许存储嫌疑人的姓名

suspect = {'Height':6,'Colour':'Fair','Residency':'Pune'}


suspect_list = []
continu = "Y"
while(continu == "Y"):
    Name = input('Please enter your name: ')
    Verification = int(input('Please enter Height: '))
    if Verification == suspect['Height']:
        print('Suspect is detected  for 1st level')
        suspect_list.append(Name)
    else:
        print('Suspect is not detected  for 1st level')
    continu = str(input("Do you want continu (Y/N): "))

【讨论】:

    【解决方案2】:

    这应该可以解决它:

    使用list(dict) 获取所有键值。

    代码

    //#colour --> color
    suspect = {"Name":"Obama","Height":6,'Color':'Black','Residency':'Pune'}
    
    //#List on a dict gives all key strings
    suspect_depictions =(list(suspect))
    
    floor = 1
    run = True
    while run:
        //#Enter info
        Name = input('Please enter your name: ')
        Height = int(input('Please enter Height: '))
        Color = input('Please enter your color: ')
        Residency = input('Please enter your residency: ')
        Still_a_suspect = True
    
        //#Check all depictions
        for depiction in suspect_depictions:
            if depiction == "Name":
                if suspect[depiction] == Name:
                    print("Name match")
                else:
                    print("Name dose not match")
                    Still_a_suspect = False
    
            if depiction == "Height":
                if suspect[depiction] == Height:
                    print("Height match")
                else:
                    print("Height dose not match")
                    Still_a_suspect = False
    
            if depiction == "Color":
                if suspect[depiction] == Color:
                    print("Color match")
                else:
                    print("Color dose not match")
                    Still_a_suspect = False
    
            if depiction == "Residency":
                if suspect[depiction] == Residency:
                    print("Residency match")
                else:
                    print("Residency dose not match")
                    Still_a_suspect = False
    
        if Still_a_suspect:
            run = False
            print("The suspect lives on the " +str(floor) + "th floor!")
        else:
            floor += 1
            print("")
            print("------------------------")
            print("")
    

    【讨论】: