Michael Hanson
Michael Hanson

Reputation: 33

writing a simple Python program

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

Upvotes: 3

Views: 548

Answers (4)

abarnert
abarnert

Reputation: 366103

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.

Upvotes: 0

mhawke
mhawke

Reputation: 87134

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)

Upvotes: 1

hjpotter92
hjpotter92

Reputation: 80657

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)

Upvotes: 3

TimothyAWiseman
TimothyAWiseman

Reputation: 14883

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

Upvotes: 0

Related Questions