Python Forum
python dictionary for students records
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python dictionary for students records
#1

Hi. i am trying to create a program in python 3 where it will ask the user to input the students name(name) and his/hers ID numbers(ID) all in one go separated by comma. Then present all the dictionary values in the following format eg:
Name:John ID:123
Name:Mary ID:234
Name:Eve ID:345
the program will run forever unless the user inputs q to exit

up to now i managed to create the following:
    addName = raw_input("New Name:")
    addid = raw_input("ID:")
    dictionary = {}
    dictionary[addName] = addid
    print('Name: ' + addName + '  ID: ' +  addid)
i would like to have the program run continuesly, unless the user enters q to exit. How can i do the input in one line so the user will input its name and then its id, separated by comma? Also print all Names and IDs that the dictionary contains.
Reply
#2
while True:
  name = input("Please input the name or q to exit: ")
  if name == 'q':
      break

  ...
  ...
  ...
Reply
#3
OK, and how can i do the input in one line so the user will input its name and then its id, separated by comma? At the end present all the entries of the dictionary in the aforementioned format, please.
Reply
#4
str = "name, id"
lis = str.split(',')
print(lis)
Reply
#5
(Nov-10-2017, 04:23 PM)heiner55 Wrote:
str = "name, id"
lis = str.split(',')
print(lis)

Hi. thanks a lot for the reply. i am confused how to join all together and then ask the user to input ID, name separated by coma, then print the current dictionary in one line. The program needs to continue asking the user to input unless 'q' is pressed. i have it here but it keeps asking without printing.
#EMPTY dictionary
students_DB={}
#initialize the input
student_number=None


#ask input from user or 'q' to exit
while True:
    student_number=input('Please input the ID and name separated by comma or q to exit: ')
    if student_number=='q':
        break
    
AM, name = your_input.split(',')
students_DB.update({AM:name})

for i in students_DB:
    print(key + ' ' + students_DB)
    print(print('{}\'s birthday is {}.'.format(AM, students_DB[name])))
print(students_DB)
what am doing wrong?
Reply
#6
#!/usr/bin/python3
students_DB={}
student_number=None

#ask input from user or 'q' to exit
while True:
    line = input('Please input the ID and name separated by comma or q to exit: ')
    if line == 'q':
        break

    id, name = line.split(',')
    students_DB.update({id:name})

#output
for key in students_DB:
    print('{}\'s name is {}.'.format(key, students_DB[key]))
Reply
#7
Thanks for the reply, but it keeps asking to input, it does not print the values up to now of the dictionary as in:
Name:John ID:123
Name:Mary ID:234
Name:Eve ID:345
an then keep asking again for user input
Reply
#8
This is my output:

Output:
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten. h:\temp\temp>test.py Please input the ID and name separated by comma or q to exit: 123,john Please input the ID and name separated by comma or q to exit: 243,mary Please input the ID and name separated by comma or q to exit: q 123's name is john. 243's name is mary. h:\temp\temp>
Reply
#9
It needs to show the contents of the dictionary without pressing q to exit. Everytme you time an id and a name then show all the contents of the dictionary and then ask the user to type again or press q to exit
Reply
#10
Ok, I understood, but you should also code something.
And moving code into the loop, that is the easy part.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Top 3 students banidjamali 4 2,360 Jan-17-2021, 11:59 AM
Last Post: banidjamali
  python program that calculat grades of students biligorm 3 2,240 Apr-04-2020, 10:11 AM
Last Post: buran

Forum Jump:

User Panel Messages

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