-1

how would i search my dictionary to see If child score is greater or equal than 100 (they should only get 8 presents), between 50 and 100 (get 5) and below 50 (get 2)

People={"Dan":22,
        "Matt":54,
        "Harry":78,
        "Bob":91}

def displayMenu():
    print("1. Add presents")
    print("2. Show score of child")
    print("3. Add Student")
    print("4. Delete students")
    print("5. Show text File: ") 
    print("6. Quit")
    choice = int(input("Enter your choice : "))
    while 6< choice or choice< 1:
        choice = int(input("Invalid. Re-enter your choice: "))
    return choice

def addpresents():
    name= input('Enter child for their score: ')
    if name in People:
        print(People[name])


option = displayMenu()

while option != 6:
    if option == 1:
       addpresents()
    if option == 3:
        addstudents()
    if option == 4:
        delete()
    if option== 5:
        txtfile()
    elif option == 2:
        print("Program terminating")

option = displayMenu()
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Waheed Hussain
  • 179
  • 1
  • 2
  • 7
  • 2
    Can you explain what it is your code is doing that is not meeting expectation? Narrow down to a [mcve] to help the reader get to your problem faster. At this point, you are asking the reader to take your code, run it, and troubleshoot it fully. You should point the area of failure. – idjaw Dec 23 '16 at 17:59

2 Answers2

3

not sure if i understood correctly, but if you want to create a dictionary based on an input dictionary and a condition, you can use a dict-comprehension:

People={"Dan":122,
        "Matt":54,
        "Harry":78,
        "Bob":91}

too_many = {key: value for key, value in People.items() if value >= 100}
print(too_many)  # -> {'Dan': 122}
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
0

You can loop through the dictionary to select the person and the score:

People={"Dan":22,"Matt":54,"Harry":78,"Bob":91}
for person,score in People.items():
    print (person,score)
# output:
# Dan 22
# Harry 78
# Matt 54
# Bob 91

Use if in the loop to select correct number of presents:

People={"Dan":22,"Matt":54,"Harry":78,"Bob":91}
for person,score in People.items():
    if score>100:
       print (person,'8 presents')
    elif score<50:
       print (person,'2 presents')
    else: 
       print (person,'5 presents')
# output:
# Dan 2 presents
# Harry 5 presents
# Matt 5 presents
# Bob 5 presents
Mahdi
  • 3,188
  • 2
  • 20
  • 33