Создание и печать содержимого словаря из введенного пользователем ввода - PullRequest
0 голосов
/ 05 сентября 2018

Я выучил основные понятия Списка, Кортежей и Словари вместе с условным утверждением. И начал писать простой кусок кода, чтобы принять ввод пользователя и создать словарь из введенных вводов и распечатать содержимое словаря.

Код написан -

patientCount = 0

noOfPatients = input("\nHow many Patient's to Admit: ")

while (patientCount < int(noOfPatients)):
    print("\nEnter the Details of the Patient {} :".format(patientCount+1))
    patientFirstName = input("{:>30}".format("Enter the FIRST NAME: "))
    patientLastName = input("{:>30}".format("Enter the LAST NAME: "))
    patientMRN = input("{:>30}".format("Enter the MRN: "))
    patientGender = input("{:>30}".format("Enter the GENDER (M/F/O): "))
    patientBirthYear = input("{:>30}".format("Enter the BIRTH YEAR: "))
    patientAge = input("{:>30}".format("Enter the AGE: "))

    patientCount +=1

КОНСОЛЬНЫЙ ВЫХОД:

How many Patient's to Admit ?: 2

Enter the Details of the Patient 1 :

    Enter the FIRST NAME: David
     Enter the LAST NAME: John
           Enter the MRN: 878783
Enter the GENDER (M/F/O): M
Enter the BIRTH YEAR (YYYY): 1901
          Patient AGE is: 117 Years
-------------------------

Enter the Details of the Patient 2 :

    Enter the FIRST NAME: Sam
     Enter the LAST NAME: Tommy
           Enter the MRN: 76487236
Enter the GENDER (M/F/O): F
Enter the BIRTH YEAR (YYYY): 1990
          Patient AGE is: 28 Years

Создан начальный пустой словарь как -

patientDatabase = {}

Я хочу создать вложенный словарь, как показано ниже из введенных выше вводов в коде -

patientDatabase = { 
Patient 1:{'First Name':'David', 'Last Name': 'John', 
'MRN': 878783, 'Gender': 'M', BirthYear': 1901, 'Age': 117}, 
Patient2:{'First Name':'Sam', 'Last Name': 'Tommy', 
'MRN': 76487236, 'Gender': 'F', BirthYear': 1990, 'Age': 28} }

А когда напечатан вышеупомянутый словарь, ВЫХОД, который я ищу, как показано ниже -

Patient 1 Details:
--------------------
FIRST NAME: David
 LAST NAME: John
       MRN: 878783
    GENDER: M
BIRTH YEAR: 1901
       AGE: 117 Years

Patient 2 Details:
--------------------
FIRST NAME: Sam
 LAST NAME: Tommy
       MRN: 76487236
    GENDER: F
BIRTH YEAR: 1990
       AGE: 28 Years

Может ли кто-нибудь помочь мне?

1 Ответ

0 голосов
/ 05 сентября 2018
    noOfPatients = input("\nHow many Patient's to Admit: ")
    patient_db=dict()
    for i in range(int(noOfPatients)):
        patien_details=dict()    
        print("\nEnter the Details of the Patient {} :".format(patientCount+1))
        patien_details["FirstName"] = input("{:>30}".format("Enter the FIRST NAME: "))
        patien_details["LastName"] = input("{:>30}".format("Enter the LAST NAME: "))
        patien_details["MRN"] = input("{:>30}".format("Enter the MRN: "))
        patien_details["Gender"] = input("{:>30}".format("Enter the GENDER (M/F/O): "))
        patien_details["BirthYear"] = input("{:>30}".format("Enter the BIRTH YEAR: "))
        patien_details["Age"] = input("{:>30}".format("Enter the AGE: "))
        patient_db[i+1]=patien_details

    for k,v in patient_db.items():
        print(k,v)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...