0

I am making a hospital management system in which there is a specific function readpatientrec in which it takes in a number and checks if the number matches the patient number and displays the record corresponding to the patient number.

I wrote an if condition for it and the patient number and inputted number matches but the if condition does not get started.

def searchpatientrec(p_no):
    f = open("patient.dat", "rb")
    flag = False
    try:
        rec = pickle.load(f)
        print(rec[0])
        print(p_no)
        if rec[0] == p_no:
            p_no = rec[0]
            p_name = rec[1]
            p_age = rec[2]
            p_problems = rec[3]
            print(f"Doctor No:{p_no}Name:{p_name},Age:{p_age},Disease:{p_problems}")
            flag = True
        else:
            pass

    except EOFError:
        pass
    
    if not flag:
        print("No Record Found")
    f.close()

Output:

1. Registering Patient details
2. Registering Doctor details
3. Registering Worker details
4. Total patient details
5. Total doctor details
6. Total worker details
7. Specific Patient detail
8. Specific Doctor detail
9. Specific Worker detail
10. Exit
Enter your choice:7
Enter patient no:1
1
1
No Record Found

Clearly rec[0] = p_no then why does the if statement not initiate?

I was expecting a match to happen and the patient details to be shown.

Here's the full code:

import pickle


def insertpatientrec():
    ch = 'y'
    while ch == 'y':
        p_no = int(input('Enter Patient number:'))
        p_name = input('Enter Patient Name:')
        p_age = int(input('Enter Age:'))
        p_problems = input('Enter the Problem/Disease:')
        rec = [p_no, p_name, p_age, p_problems]
        f = open('patient.dat', 'ab')
        pickle.dump(rec, f)
        print("Record Added")
        f.close()

        ch = input("Do you want to add more records? (y/n):")


def insertdoctorrec():
    ch = 'y'
    while ch == 'y':
        d_no = int(input('Enter Doctor number:'))
        d_name = input('Enter Doctor Name:')
        d_age = int(input('Enter Age:'))
        d_department = input('Enter the Department:')
        rec = [d_no, d_name, d_age, d_department]
        f = open('doctor.dat', 'ab')
        pickle.dump(rec, f)
        print("Record Added")
        f.close()

        ch = input("Do you want to add more records? (y/n):")


def insertworkerrec():
    ch = 'y'
    while ch == 'y':
        w_no = int(input('Enter Worker number:'))
        w_name = input('Enter Worker Name:')
        w_age = int(input('Enter Age:'))
        w_workname = input('Enter type of work:')
        rec = [w_no, w_name, w_age, w_workname]
        f = open('worker.dat', 'ab')
        pickle.dump(rec, f)
        print("Record Added")
        f.close()

        ch = input("Do you want to add more records? (y/n):")


def readpatientrec():
    f = open("patient.dat", "rb")
    while True:
        try:
            rec = pickle.load(f)
            p_no = rec[0]
            p_name = rec[1]
            p_age = rec[2]
            p_problems = rec[3]
            print(f"\nPatient No:{p_no}\tName:{p_name}\tAge:{p_age}\tDisease:{p_problems}")
        except EOFError:
            break
    f.close()


def readdoctorrec():
    f = open("doctor.dat", "rb")
    while True:
        try:
            rec = pickle.load(f)
            d_no = rec[0]
            d_name = rec[1]
            d_age = rec[2]
            d_department = rec[3]
            print(f"\nDoctor No:{d_no}\tName:{d_name}\tAge:{d_age}\tDisease:{d_department}")
        except EOFError:
            break
    f.close()


def readworkerrec():
    f = open("worker.dat", "rb")
    while True:
        try:
            rec = pickle.load(f)
            w_no = rec[0]
            w_name = rec[1]
            w_age = rec[2]
            w_workname = rec[3]
            print(f"\nWorker No:{w_no}\tName:{w_name}\tAge:{w_age}\tDisease:{w_workname}")
        except EOFError:
            break
    f.close()


def searchpatientrec(p_no):
    f = open("patient.dat", "rb")
    flag = False
    try:
        rec = pickle.load(f)
        print(rec[0])
        print(p_no)
        if rec[0] == p_no:
            p_no = rec[0]
            p_name = rec[1]
            p_age = rec[2]
            p_problems = rec[3]
            print(f"Doctor No:{p_no}Name:{p_name},Age:{p_age},Disease:{p_problems}")
            flag = True
        else:
            pass

    except EOFError:
        pass

    if not flag:
        print("No Record Found")
    f.close()


def searchdoctorrec(d_no):
    f = open("doctor.dat", "rb")
    flag = False
    try:
        rec = pickle.load(f)
        if rec[0] == d_no:
            d_no = rec[0]
            d_name = rec[1]
            d_age = rec[2]
            d_department = rec[3]
            print(f"Doctor No:{d_no}Name:{d_name},Age:{d_age},Disease:{d_department}")
            flag = True
        else:
            pass

    except EOFError:
        pass
    if not flag:
        print("No Record Found")
    f.close()


def searchworkerrec(w_no):
    f = open("patient.dat", "rb")
    flag = False
    try:
        rec = pickle.load(f)
        if rec[0] == w_no:
            w_no = rec[0]
            w_name = rec[1]
            w_age = rec[2]
            w_workname = rec[3]
            print(f"Worker No:{w_no}Name:{w_name},Age:{w_age},Disease:{w_workname}")
            flag = True
        else:
            pass

    except EOFError:
        pass
    if not flag:
        print("No Record Found")
    f.close()


while True:
    print('---------------------------------------------')
    print("HOSPITAL MANAGEMENT SYSTEM")
    print('---------------------------------------------')
    print("1.Login")
    print("2.Exit")
    choice = int(input("Enter your choice:"))
    if choice == 1:
        username = input("enter user name:")
        password = input("enter the password:")
        print('Connecting to Hotel Management system...')
        while username == 'a' and password == 'a':
            print('\n1. Registering Patient details')
            print('2. Registering Doctor details')
            print('3. Registering Worker details')
            print("4. Total patient details")
            print("5. Total doctor details")
            print("6. Total worker details")
            print('7. Specific Patient detail')
            print('8. Specific Doctor detail')
            print('9. Specific Worker detail')
            print('10. Exit')
            choice = int(input('Enter your choice:'))
            if choice == 1:
                insertpatientrec()

            elif choice == 2:
                insertdoctorrec()

            elif choice == 3:
                insertworkerrec()

            elif choice == 4:
                readpatientrec()

            elif choice == 5:
                readdoctorrec()

            elif choice == 6:
                readworkerrec()

            elif choice == 7:
                no = input("Enter patient no:")
                searchpatientrec(no)

            elif choice == 8:
                no = input("Enter doctor no:")
                searchdoctorrec(no)

            elif choice == 9:
                no = input("Enter worker no:")
                searchworkerrec(no)

            elif choice == 10:
                break

        else:
            print('Wrong username or password')

    if choice == 2:
        break
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • I guess an exception is raised and you silenced it. – Klaus D. Nov 06 '22 at 16:50
  • Please try two things: (1) add `print('equal')` above `p_no = rec[0]` - do you see the output "equal"? (2) replace `except EOFError: pass` to `except EOFError: raise` - what happens? – mkrieger1 Nov 06 '22 at 16:53
  • @mkrieger1 Tried both the things but the output remains the same... – Aditya Chauhan Nov 06 '22 at 16:59
  • What's the output if you change `print(rec[0])` and `print(p_no)` to `print(repr(rec[0]))` and `print(repr(p_no))`? And what's the output of `print(repr(rec))`? – mkrieger1 Nov 06 '22 at 18:09
  • [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) / [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) – mkrieger1 Nov 06 '22 at 18:12

1 Answers1

-1

as Klaus D. said, you could try the following code, to catch the exception and find the solution

def searchpatientrec(p_no):
f = open("patient.dat", "rb")
flag = False
try:
    rec = pickle.load(f)
    print(rec[0])
    print(p_no)
    if rec[0] == p_no:
        p_no = rec[0]
        p_name = rec[1]
        p_age = rec[2]
        p_problems = rec[3]
        print(f"Doctor No:{p_no}Name:{p_name},Age:{p_age},Disease:{p_problems}")
        flag = True
    else:
        pass

except Exception as e:
    raise(f"error: {e}")

if not flag:
    print("No Record Found")
f.close()
Andres Ospina
  • 108
  • 1
  • 2
  • 8