函数获取python中的动物数量 [英] Function to get the number of animals in python
问题描述
你好这些函数根据这两只动物的头和腿的输入返回猪和鸡的数量。我对Python非常陌生。我无法理解它是如何工作的。你能详细解释一下吗?
def solve(numLegs,numHeads):
在numChick范围内(0,numHeads + 1):
numPigs = numHeads - numChicks
totLegs = 4 * numPigs + 2 * numChicks
如果totLegs == numLegs:
返回[numPigs,numChicks]
return [None,None]
$ b $ def barnYard():
heads = int(raw_input('输入头数:'))
legs = int(raw_input('输入腿数:') )
猪,鸡=解决(腿,头)
如果猪=无:
打印'没有解决方案'
其他:
print'猪的数量:',pigs
pirnt'chickes数量:',chickens
所以当我运行谷仓功能它问头数,所以我把20头,然后把56腿。它打印猪的数量8和数量鸡12.但我真的不明白它是如何达到这个阶段。特别是 solve()
函数。帮助将不胜感激。
基本上:
def solve(numLegs,numHeads):
在numChick范围内(0,numHeads + 1):#对于范围0中的每个数字 - 头数+ 1,numChick =该数字
numPigs = numHeads - numChicks#猪的数量等于输入的头数减去当前数字
totLegs = 4 * numPigs + 2 * numChicks#腿的数量是4(腿的数量)*头数+ 2 (鸡腿)*当前数字
如果totLegs == numLegs:如果猪腿+鸡腿=腿的总数,
返回[numPigs,numChicks]#返回猪的数量和鸡
return [None,None] #else,返回none,触发no solution
$ b $ def barnYard():
heads = int(raw_input('Enter the number of heads :'))#
legs = int(raw_input('输入腿数:'))
pigs,chickens = solve(legs,heads)
if pigs = No ne:
print'没有解决方案'
else:
print'猪的数量:',猪
pirnt'鸡的数量:',鸡
所以:
基本上,它会运行该函数并且直到计算出的腿的数量等于输入的腿的总量。如果它永远不等于,它只会返回[none,none]。 (返回[numPigs,numChicks]打破for循环)
编辑:
我试着拿出第2行的+1它仍然工作得很好。
Hello these functions return the number of pigs and chickens based on the input of these two animals' heads and legs. I am really new to python. I can't understand how it works. Can you explain this elaborately?
def solve(numLegs, numHeads):
for numChick in range(0, numHeads + 1):
numPigs = numHeads - numChicks
totLegs = 4 * numPigs + 2* numChicks
if totLegs == numLegs:
return [numPigs, numChicks]
return[None, None]
def barnYard():
heads = int(raw_input('Enter number of heads:'))
legs = int(raw_input('Enter number of legs:'))
pigs, chickens = solve(legs, heads)
if pigs = None:
print 'there is no solution'
else:
print 'number of pigs:' , pigs
pirnt 'number of chickes:', chickens
So when i run the barnYard function it asks number of heads so i put 20 heads and then put 56 legs. It prints number of pigs 8 and number chickens 12. But i really can't understand how it reaches at this stage. Specially the solve()
function. Help will be appreciated.
Basically:
def solve(numLegs, numHeads):
for numChick in range(0, numHeads + 1): #for every number in the range 0 - the number of heads + 1, numChick = that number
numPigs = numHeads - numChicks #the number of Pigs equals the number of heads entered minus the current number
totLegs = 4 * numPigs + 2* numChicks #the amount of legs is 4(amount of legs) * number of heads + 2(chicken legs) * the current number
if totLegs == numLegs: if the pigs legs + the chicken legs = the total number of legs,
return [numPigs, numChicks] #return the number of pigs and chickens
return[None, None] #else, return none, triggering "no solution"
def barnYard():
heads = int(raw_input('Enter number of heads:')) #
legs = int(raw_input('Enter number of legs:'))
pigs, chickens = solve(legs, heads)
if pigs = None:
print 'there is no solution'
else:
print 'number of pigs:' , pigs
pirnt 'number of chickes:', chickens
So:
Basically, it will run the function over and over until the amount of legs calculated equals the total amount of legs entered. If it never equals up, it just returns [none, none]. (return [numPigs, numChicks] breaks the for loop)
Edit: I tried taking out the +1 in line 2 and it still worked just fine.
这篇关于函数获取python中的动物数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!