我正在尝试制作一个程序,允许用户输入孩子的数量,并且该程序计算出它需要多少辆公交车(不同尺寸的公交车)以及哪种组合是最好的,但是我一直在努力进行改进在您获得原始数量的公交车之后。我似乎无法在函数中再次使用剩余的子项。
到目前为止的代码:
children = int(input('Enter number of children: ')
bus1 = 10
cost1 = 100
bus2 = 30
cost2 = 200
bus3 = 60
cost3 = 300
val1 = 0
left1 = 0
val2 = 0
left2 = 0
val3 = 0
left3 = 0
#buses needed
def getValues(children, left1 , left2 , left3):
val1 = children//bus1
left1 = children % bus1
val2 = children//bus2
left2 = children % bus2
val3 = children//bus3
left3 = children % bus3
print(val1 , 'buses with ' , left1 , ' left\n', val2 , ' buses with ' , left2 , ' left\n', val3 ,'buses with ', left3, 'left\n')
if left1 > 0:
children = left1
left1 -= children
getValues(children , left1 , left2 , left3)
else:
solution1 = (val1 , 'buses with ' , left1 , ' left\n')
if left2 > 0:
children = left2
left2 -= children
getValues(children, left1 , left2 , left3)
else:
solution2 = (val2 , 'buses with ' , left2 , ' left\n')
if __name__ == '__main__':
getValues(children , left1 , left2 , left3)