编写一个简单的Python程序

2024-06-07 01:30:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我在为初学者编程类编写一个简单的Python程序时遇到了困难。如果有人可以看看我目前的代码,并指导我在正确的方向,它将不胜感激!代码如下:

帮助满足护士到病人的人员配置需求的计划

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

Tags: oftheto代码numberreturnismain
3条回答

我注意到两件事:

首先,你的代码没有缩进。这可能只是复制到该站点的方式的一个后遗症,但请记住,在Python中空白是非常重要的。特别是,代码块是通过缩进级别来标识的。例如,main()函数应该如下所示:

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

函数内部的所有内容都缩进。根据您使用的IDE,您可能只需突出显示内容,然后按tab按钮。在

下一个,也是不那么重要的一点是,你正在使用浮动来处理诸如保姆数量之类的事情。因为很难有一个护士的一小部分,所以您可能需要使用类似ceil()的方法将其提升到下一个整数

将最后一段代码更改为:

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

因为python是基于缩进执行的。另外,从printInfo语句中删除=,并使其:

^{pr2}$

之前的答案已经暗示了主要问题,但没有解释为什么没有看到任何输出。在

下面的将包含患者、护士和护士助理的3个值的atuple分配给名为printInfo的变量。在

printInfo = (patients, nurses, nurseAssistants)

它不会产生任何输出,也不会像您预期的那样调用函数printInfo()。您实际需要的是进行函数调用:

^{pr2}$

相关问题 更多 >