首页 > 编程语言 >异常处理及程序调试

异常处理及程序调试

时间:2022-12-16 01:44:20浏览次数:44  
标签:__ apple 处理 程序调试 result print 异常 children 苹果

def division():
    print('\n==========分苹果了===========\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__':
division()

def division():
    print('\n==========分苹果了===========\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:
        division()
    except ZeroDivisionError:
        print('\n出错了~_~-苹果不能被0个小朋友分!')

def division():
    print('\n==========分苹果了===========\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:
        division()
    except ZeroDivisionError:
        print('\n出错了~_~-苹果不能被0个小朋友分!')
    except ValueError as e:
        print('\n出错了~_~-',e)

def division():
    print('\n==========分苹果了===========\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,'个。')
try:
        division() 
except AssertionError as e:
        print('输入有误!',e)

标签:__,apple,处理,程序调试,result,print,异常,children,苹果
From: https://www.cnblogs.com/whc123/p/16986378.html

相关文章