Python Forum
homework hospital charges program help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
homework hospital charges program help
#1
Hi, guys. Im working on a program for a class. Its an inpatient or outpatient hospital charges program. here is what ive gotten so far.

def inPatientFunction(hospitalDailyRate,numOfDaysInHospital,medicationCharges,hospitalServicesCharge):
    totalPatientCharges=hospitalDailyRate*numOfDaysInHospital*(medicationCharges+hospitalServicesCharge)
    return totalPatientCharges
#creating function for an outpatient   
def outPatientFunction(medicationCharges,hospitalServicesCharge):
    totalPatientCharges=medicationCharges+hospitalServicesCharge
    return totalPatientCharges
#creating function to run a loop for type of patient charges
def checkPatientType():
    if (patientType==0):
        while True:
            try:
                hospitalServicesCharge=float(input('Please enter the hospital service charges for your care $  '))
                medicationCharges=float(input('Please enter the charges for the hospital medication $  '))
                if (hospitalServicesCharge>=0 and medicationCharges>=0):
                    break
            except ValueError:
                print('Invalid entry, try again')
            
            else:
                while True:
                    try:
                        hospitalDailyRate=float(input ('Please enter the daily charge for the hospital stay $ '))
                        numOfDaysInHospital=int(input ('How many days in total was the hopsital stay? '))
                        hospitalServicesCharge=float(input ('Please enter the hospital service charges for your care $ '))
                        medicationCharges=float(input ('Please enter the charges for the hospital medication $ '))
                        if (hospitalServicesCharge>=0 and medicationCharges>=0 and hospitalDailyRate>=0 and numOfDaysInHospital>=0):
                            break
                    except ValueError:
                        print('Invalid entry, try again')
                        
#creating conditional statement to call correct function
    if (patientType):
        return inPatientFunction(hospitalDailyRate,numOfDaysInHospital,medicationCharges,hospitalServicesCharge)
    else:
        return outPatientFunction(medicationCharges,hospitalServicesCharge)
#establishing variables at zero          
patientType=0
totalPatientCharges=0
#asking user for type of patient
typeOfPatient=input('Is this an inpatient or outpatient? ')
if (typeOfPatient=='inpatient'):
    patientType=1
    
#assigning a value to the total patient charges variable depending on what function has been used 
totalPatientCharges= checkPatientType()
#showing user the total amount for hospital charges
print ('Total hospital charges are $ ', format(totalPatientCharges,'.2f'))
Towards the end when I ask the user if theyre an inpatient or outpatient, instead of doing a string comparison which is prone to user typos, is there a more fool proof way to ask the user this question? like press 1 for inpatient or press 0 for outpatient and reject all other inputs? and if I do change it to that style of input, what part of my code will I have to change? Because it runs good right now,I just want to get rid of that possible typo factor

Any other critiques of my overall code will be absolutely welcomed. Thanks.
Reply
#2
This would be easier to read if you format it a bit differently:
def inPatientFunction(hospitalDailyRate, numOfDaysInHospital, medicationCharges, hospitalServicesCharge):
    totalPatientCharges = hospitalDailyRate*numOfDaysInHospital * \
        (medicationCharges+hospitalServicesCharge)
    return totalPatientCharges

# creating function for an outpatient
def outPatientFunction(medicationCharges, hospitalServicesCharge):
    totalPatientCharges = medicationCharges+hospitalServicesCharge
    return totalPatientCharges

# creating function to run a loop for type of patient charges
def checkPatientType():
    if (patientType == 0):
        while True:
            try:
                hospitalServicesCharge = float(
                    input('Please enter the hospital service charges for your care $  '))
                medicationCharges = float(
                    input('Please enter the charges for the hospital medication $  '))
                if (hospitalServicesCharge >= 0 and medicationCharges >= 0):
                    break
            except ValueError:
                print('Invalid entry, try again')

            else:
                while True:
                    try:
                        hospitalDailyRate = float(
                            input('Please enter the daily charge for the hospital stay $ '))
                        numOfDaysInHospital = int(
                            input('How many days in total was the hopsital stay? '))
                        hospitalServicesCharge = float(
                            input('Please enter the hospital service charges for your care $ '))
                        medicationCharges = float(
                            input('Please enter the charges for the hospital medication $ '))
                        if (hospitalServicesCharge >= 0 and medicationCharges >= 0 and hospitalDailyRate >= 0 and numOfDaysInHospital >= 0):
                            break
                    except ValueError:
                        print('Invalid entry, try again')

    # creating conditional statement to call correct function
    if (patientType):
        return inPatientFunction(hospitalDailyRate, numOfDaysInHospital, medicationCharges, hospitalServicesCharge)
    else:
        return outPatientFunction(medicationCharges, hospitalServicesCharge)


# establishing variables at zero
patientType = 0
totalPatientCharges = 0

# asking user for type of patient
typeOfPatient = input('Is this an inpatient or outpatient? ')
if (typeOfPatient == 'inpatient'):
    patientType = 1

# assigning a value to the total patient charges variable depending on what function has been used
totalPatientCharges = checkPatientType()

# showing user the total amount for hospital charges
print('Total hospital charges are $ ', format(totalPatientCharges, '.2f'))
This is still not the greatest, but a bit better.

Since there are only two options, you can just use something like (untested code):
while True:
    choice = input('Please choose (I) for inpatient, (O) for outpatient')
    if choice == 'I' or choice == 'O'
        break
Reply
#3
For your inPatientFunction and outPatientFunction, you can simply return the calculation without assigning a variable. This is more concise and if the variable isn't needed for anything, there's no reason to make it.

def inPatientFunction(hospitalDailyRate, numOfDaysInHospital, medicationCharges, hospitalServicesCharge):
    return hospitalDailyRate * numOfDaysInHospital * (medicationCharges + hospitalServicesCharge)
    
#creating function for an outpatient   
def outPatientFunction(medicationCharges, hospitalServicesCharge):
    return medicationCharges + hospitalServicesCharge
For your conditionals, if the logic statement is only a single line, you don't need the parentheses.

Thinking about the user experience, just imagine how annoying it would be to enter a typo when inputting the medicationCharges. It's either negative or a non-numeric character. You'd have to re-enter the other three again just to have another chance to get it right (and hope for no other typos). To avoid that, you can move your input and input tests into a function instead. That way, if a single entry is wrong, the user is immediately notified and gets a chance to correct it.
Reply
#4
(Oct-20-2018, 02:55 AM)stullis Wrote: Thinking about the user experience, just imagine how annoying it would be to enter a typo when inputting the medicationCharges. It's either negative or a non-numeric character. You'd have to re-enter the other three again just to have another chance to get it right (and hope for no other typos). To avoid that, you can move your input and input tests into a function instead. That way, if a single entry is wrong, the user is immediately notified and gets a chance to correct it.


I know, I cant figure out how to not revert to the first input after any value error. can you give me an example of how you would set up that function in this program?
Reply
#5
Just reorganize the code you already have for it:

def user_input(message):
    while True:
        try:
            amount = float(input(message))

            if amount >= 0:
                return amount
            else: print("Value must be 0 or higher.")

        except ValueError:
            print("Invalid value, please enter a number.")
Then, you just need to change your variable instantiation to:

hospitalServicesCharge = user_input('Please enter the hospital service charges for your care $  ')
Reply
#6
many text mode programs output a numbered list of options (typically starting at 1) and ask for the number to be typed in. a GUI would have check boxes or a drop down menu. some text programs even do this when they can. and some even output the choice that was made and ask to confirm it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Creating Program Homework ShakenBake 2 2,985 Feb-10-2018, 11:42 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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