Как использовать l oop, когда тип данных - словарь - PullRequest
0 голосов
/ 04 марта 2020
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'} обнаружено suspect, имя должно быть сохранено, а код продолжен. Это означает, что 'Please enter your name' и 'Please Enter Height' следует продолжать спрашивать.

Ответы [ 2 ]

0 голосов
/ 04 марта 2020

Это должно исправить:

Используйте 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("")
0 голосов
/ 04 марта 2020

Вам нужно использовать какое-то время l oop. При каждом l oop программа спрашивает, хотите ли вы продолжить. Кроме того, список позволяет хранить имена подозреваемых

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): "))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...