Python Forum
printing data from dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing data from dictionary
#1
Quote:NOTE: modifying my questions below as I was able to fix the issue with my original post but am now having additional problems.

I'm trying to write code that will collect the number of individuals attending an event. Based on the number of people entered, I will ask that many times a) the name of the individual, b) the number of toys they have donated. I want the information to be entered into a dictionary which i will pass to another function. The second function will select individuals who have donated more than 20 toys and print their name and the number of toys the donated as "level 1" and then everyone else as "level 2".

I've checked to make sure that the data is being passed from one function to another correctly. I'm having issues printing the donation amount without the []. Also, it keeps printing the level above each entry instead of listing the entries underneath the level. In other words, I'm trying to get:

Level 1
Selena Gomez 50
Beyonce 40

Level 2
Will Smith 5

What am I doing wrong?

Here is my code:
def data():
    people = int(input("How many individuals are attending? "))
    attendees = {}
    for i in range(people):
        name = str(input('Please enter name: '))
        amount = int(input('Number of toys donated:'))
        attendees[name]= [amount]

    return attendees
    

    

def print_data():
    attendees = data()
    
    for i in attendees:
        if attendees[i][0] > 20:
            print('Level 1')
            print(i, attendees[i])
           
        else:    
            print('Level 2')
            print(i, attendees[i])
 
print_data()
And here is my output:
Error:
How many individuals are attending? 3 Please enter name: Will Smith Number of toys donated:5 Please enter name: Selena Gomez Number of toys donated:50 Please enter name: Beyonce Number of toys donated:40 Level 2 Will Smith [5] Level 1 Selena Gomez [50] Level 1 Beyonce [40]
Reply
#2
In your function data you add number of toys donated as list (line#7). Edit this part of your code.
Reply
#3
def data():
    people = int(input("How many individuals are attending? "))
    attendees = {}
    for i in range(people):
        name = str(input('Please enter name: '))
        amount = int(input('Number of toys donated:'))
        attendees[name] = amount
    return attendees

def print_data():
    attendees = data()

    print('Level 1')
    for i in attendees:
        if attendees[i] > 20:
            print(i, attendees[i])

    print('Level 2')
    for i in attendees:
        if attendees[i] <= 20:
            print(i, attendees[i])

print_data()
Reply
#4
(Nov-20-2017, 03:31 PM)heiner55 Wrote:
def data():
    people = int(input("How many individuals are attending? "))
    attendees = {}
    for i in range(people):
        name = str(input('Please enter name: '))
        amount = int(input('Number of toys donated:'))
        attendees[name] = amount
    return attendees

def print_data():
    attendees = data()

    print('Level 1')
    for i in attendees:
        if attendees[i] > 20:
            print(i, attendees[i])

    print('Level 2')
    for i in attendees:
        if attendees[i] <= 20:
            print(i, attendees[i])

print_data()

Thanks for your reply. While this gets me closer, it also only prints on entry. When I modify my code as suggested, it only allows me to enter one name before it prints the name/level. So if I entered 3 attendees would be attending, it only gives me the chance to enter the first name and then immediately moves to the second function to print the name.

This also makes me think that I want to modify it further to only print the level name if there are entries that apply. In other words, if there are no entries that would fall into level 2, then it should not print the level 2 header.
Reply
#5
Whenever a program is finished, the next day you will  have new ideas for improvements.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,975 May-15-2020, 04:57 PM
Last Post: snippsat
  Why is this function printing the number of keys in a dictionary? mafr97 5 3,043 Sep-17-2019, 06:19 AM
Last Post: perfringo
  Presenting data from a dictionary in a basic table ijosefson 3 3,309 Oct-16-2017, 04:13 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020