作业七

1.模拟幼儿园分苹果
def divison():
print("\n============分苹果了===========")
apple = int(input("请输入苹果个数: "))
children = int(input("请输入来了几个小朋友: "))
result = apple / children
remain = apple - result*children
if remain > 0:
print(apple,'个苹果,平均分给',children,'个小朋友,每人分',result,"个,剩下",remain,'个')
else:
print(apple,'个苹果,平均分给',children,'个小朋友,每人分',result,"个")
if __name__ == '__main__':
divison()

 

 2.模拟幼儿园分苹果(除数不能为0)


def divison():
print("\n============分苹果了===========")
apple = int(input("请输入苹果个数: "))
children = int(input("请输入来了几个小朋友: "))
result = apple // children
remain = apple - result*children
if remain > 0:
print(apple,'个苹果,平均分给',children,'个小朋友,每人分',result,"个,剩下",remain,'个')
else:
print(apple,'个苹果,平均分给',children,'个小朋友,每人分',result,"个")
if __name__ == '__main__':
try:
divison()
except ZeroDivisionError:
print("\n出错了")

 

3.模拟幼儿园分苹果(每个人至少分到一个苹果)

def divison():
print("\n============分苹果了===========")
apple = int(input("请输入苹果个数: "))
children = int(input("请输入来了几个小朋友: "))
if apple < children:
raise ValueError("苹果太少了,不够分")
result = apple // children
remain = apple - result*children
if remain > 0:
print(apple,'个苹果,平均分给',children,'个小朋友,每人分',result,"个,剩下",remain,'个')
else:
print(apple,'个苹果,平均分给',children,'个小朋友,每人分',result,"个")
if __name__ == '__main__':
try:
divison()
except ZeroDivisionError:
print("\n出错了")
except ValueError as e:
print("\n出错了",e)

 

 4.模拟幼儿园分苹果(应用断言调试)

def divison():
print("\n============分苹果了===========")
apple = int(input("请输入苹果个数: "))
children = int(input("请输入来了几个小朋友: "))
assert apple >= children,"苹果不够分"
result = apple // children
remain = apple - result*children
if remain > 0:
print(apple,'个苹果,平均分给',children,'个小朋友,每人分',result,"个,剩下",remain,'个')

else:
print(apple,'个苹果,平均分给',children,'个小朋友,每人分',result,"个")
if __name__ == '__main__':
divison()

 

 

原文地址:http://www.cnblogs.com/cwt13713728438/p/16852626.html

发表评论

您的电子邮箱地址不会被公开。