Use python to solve the problem of chicken and rabbit in the same cage

Judging the validity of the number of heads

while True:

head = int(input("鸡和兔的头数和:"))

if head>0:

break

else:

    print("输入头数有误,请重新输入!")

Validate the number of feet while True:

foot = int(input("鸡和兔的脚数和:"))

if foot>=head2 and foot<=head4:

break

else:

    print("输入脚数有误,请重新输入!")

The number of chickens ji = 0

while ji<=head:

# 兔的个数tu = head - ji

if foot==ji* 2+tu*4:

    print("鸡有{}只,兔有{}只".format(ji,tu))

break

ji + = 1

print("The program ends!")

Guess you like

Origin blog.csdn.net/li_little_flower/article/details/108712419