这是我在《编程导论》一书中的一个问题,我不太明白如何在不使用Ifs的情况下做到这一点,因为我们的教授只想要基本的模和除法。我试图得到3个输出。引出序号大于子项(有效),引出序号等于仅输出0和0的子项。气球比孩子少,这不管用。在
# number of balloons
children = int(input("Enter number of balloons: "))
# number of children coming to the party
balloons = int(input("Enter the number of children coming to the party: "))
# number of balloons each child will receive
receive_balloons = int(balloons % children)
# number of balloons leftover for decorations
remaining = children % balloons
print("{:s}""{:d}""{:s}""{:d}".format("Number of balloons for each child is ", receive_balloons, " and the amount leftover is ", remaining))
print(balloons, "", (remaining))
你需要修正变量赋值,你分配给了错误的变量,并实际除数得到
receive_balloons
正确:输出(10/5):
^{pr2}$输出(10/8):
注意:在Python2.7中,您应该使用
raw_input
。在对于每个子级的引出序号,需要使用//运算符,对于剩余的引出序号,需要使用%运算符
相关问题 更多 >
编程相关推荐