3

I'm having trouble writing a simple Python program for a beginner programming class. If someone could look at the code I currently have and guide me in the right direction it would be much appreciated! Here is the code:

A program that helps fulfil nurse to patient staffing needs

def main():
    print 'Welcome to the PACU nurse to patient program'
    print
    patients = inputPatients()
    nurses = getNurses(patients)
    nurseAssistants = getAssistants(nurses)
    printInfo = (patients, nurses, nurseAssistants)
   
    raw_input()

def inputPatients():
    patients = input('Enter the number of patients for this shift (up to 40): ')
    return patients

def getNurses(patients):
    nurses = (1.0 / 3.0) * patients
    return nurses

def getAssistants(nurses):
    nurseAssistants = (1.0 / 2.0) * nurses
    return nurseAssistants

def printInfo(patients, nurses, nurseAssistants):
    print 'The number of patients for this shift is:', patients
    print 'The number of nurses needed is:', nurses
    print 'The number of nurses Assistants is:', nurseAssistants


main()
4
  • 1
    What are you having trouble with? Commented Jul 16, 2012 at 23:50
  • Ok, with the reformating inspectorG4dget has the heart of the matter. Also consider the fact that you wind up with fractions for the nurses and nurseAssistants. Commented Jul 16, 2012 at 23:57
  • I'm having trouble getting the program to run properly. Its a pretty straight forward program, there has to be an issue with my code. just not sure what I'm going wrong. Commented Jul 16, 2012 at 23:58
  • Why do you want 2.5 assistants for 15 patients? :P Commented Jul 17, 2012 at 0:00

4 Answers 4

3

Change the last segment of code to:

def printInfo(patients, nurses, nurseAssistants):
    print 'The number of patients for this shift is:', patients
    print 'The number of nurses needed is:', nurses
    print 'The number of nurses Assistants is:', nurseAssistants

main()

Since python executes based on indentation. Also, remove the = from the printInfo statement and make it:

nurses = getNurses(patients)
nurseAssistants = getAssistants(nurses)
printInfo(patients, nurses, nurseAssistants)
Sign up to request clarification or add additional context in comments.

3 Comments

Yeah, that got it running. Thank you. Now how to figure out how to program ratios scratches head.
@MichaelHanson Take the nearest multiple of 6 maybe? or round off the decimal to nearest value.
If your issue is just "how do I do ratios", the most direct answer is fractions.Fraction(1, 3), but really, (1.0 / 3.0) works fine. Ultimately, whether you use ratios or decimals or floats or manually scale up integers (as Nerd-Herd suggests), you're still going to have to add the code somewhere to round up from 2.5 assistants to 3, e.g., via nurseAssistants = math.ceil((1.0 / 2.0) * nurses).
1

Previous answers have hinted at the main problem but not explained why you are not seeing any output.

The following assigns a tuple containing the 3 values for patients, nurses, and nurse assistants to the variable named printInfo.

printInfo = (patients, nurses, nurseAssistants)

It does not produce any output and it does not call the function printInfo() as you are probably expecting. What you actually need is to make a function call:

printInfo(patients, nurses, nurseAssistants)

Comments

0

I notice two things:

First, none of your code is indented. This might just be an after effect of how you copied into this site, but remember that in Python whitespace is significant. In particular, code blocks are identified by indentation levels. So for instance your main() function should look like:

def main(): 
    print 'Welcome to the PACU nurse to patient program' 
    print 
    patients = inputPatients() 
    nurses = getNurses(patients) 
    nurseAssistants = getAssistants(nurses) 
    printInfo (patients, nurses, nurseAssistants) 

    raw_input() 

With everything inside the function indented. Depending on the IDE you are using, you can probably just highlight the contents and then press the tab button.

The next, and less significant thing, is that you are using floats for things like the number of nursers. Since its hard to have a fraction of a nurse you may want to bring this up to the next integer, using something like ceil()

2 Comments

Yeah, its an after effect of pasting the code into the site. The code in Python is indented correctly. I might be having an issue though with the second issue you bought up, I meant calculate ratios, 1 nurse for every 3 patients, and 1 nurse assistant for every 2 nurses. Any idea on how I would input ratios?
There are several options, one of them is the fractions module.
0

Nerd-Herd and mhawke have already explained the issue with printInfo and with indents, but it sounds like you're still confused by how to get 3 nurseAssistants instead of 2.5. For example, you say, "Now how to figure out how to program ratios."

By using floats, you can run into the problem that not every decimal floating-point value is exactly representable as an IEEE floating-point value. And that one can be solved by "programming ratios", e.g., by using fractions.Fraction(1, 2) instead of 1.0 / 2.0, or by using decimal floats like decimal(1) / decimal(2). But that's not actually the problem you're having.

The problem you're having is that you want to round up, and that doesn't happen automatically. You have to do it explicitly, e.g., by calling math.ceil. And there are two places you could do it, with different semantics, so you have to decide which one makes sense for your problem.

Option 1 is to round up as soon as possible, like this:

def getNurses(patients):
    nurses = math.ceil((1.0 / 3.0) * patients)
    return nurses

def getAssistants(nurses):
    nurseAssistants = math.ceil((1.0 / 2.0) * nurses)
    return nurseAssistants

Option 2 is to round up as late as possible, like this:

def printInfo(patients, nurses, nurseAssistants):
    print 'The number of patients for this shift is:', math.ceil(patients)
    print 'The number of nurses needed is:', math.ceil(nurses)
    print 'The number of nurses Assistants is:', math.ceil(nurseAssistants)

In your case, these actually will turn out to give the same answers (assuming the number of patients is always an integer, which hopefully it is…), but hopefully you can see how these can give different answers in similar cases, so you have to decide which is correct.

Meanwhile, math.ceil(2.66666) is going to print out as '3.0' when you may want '3' instead. To do that, just change math.ceil(nurses) to int(math.ceil(nurses)), etc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.