Python Forum
python dictionary for students records - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: python dictionary for students records (/thread-6192.html)

Pages: 1 2


python dictionary for students records - atux_null - Nov-10-2017


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.


RE: python dictionary for students records - heiner55 - Nov-10-2017

while True:
  name = input("Please input the name or q to exit: ")
  if name == 'q':
      break

  ...
  ...
  ...



RE: python dictionary for students records - atux_null - Nov-10-2017

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.


RE: python dictionary for students records - heiner55 - Nov-10-2017

str = "name, id"
lis = str.split(',')
print(lis)



RE: python dictionary for students records - atux_null - Nov-12-2017

(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?


RE: python dictionary for students records - heiner55 - Nov-12-2017

#!/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]))



RE: python dictionary for students records - atux_null - Nov-12-2017

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


RE: python dictionary for students records - heiner55 - Nov-12-2017

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>



RE: python dictionary for students records - atux_null - Nov-12-2017

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


RE: python dictionary for students records - heiner55 - Nov-12-2017

Ok, I understood, but you should also code something.
And moving code into the loop, that is the easy part.