鱼C论坛

 找回密码
 立即注册
查看: 614|回复: 3

[已解决]Type error 的问题

[复制链接]
发表于 2020-3-25 19:05:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
def question(heads, legs):
    rabbit_number_max = legs // 4
    rabbit_number = 0
    while True:
        chicken_number = (legs - rabbit_number * 4) / 2
        if chicken_number == int(chicken_number) and rabbit_number + chicken_number == heads:
            return (chicken_number, rabbit_number)
        elif rabbit_number <= rabbit_number_max:
            rabbit_number += 1
        else:
            print('题目错误啦*_*!此题无解!')

def number(heads, legs):
    if heads <= 0 or heads != int(heads):
        print('头的参数错误T_T!请输入一个正整数!')
        heads = float(input('请重新输入头的个数:'))
       
    elif legs <= 0 or legs != int(legs):
        print('脚的参数错误T_T!请输入一个正整数!')
        legs = float(input('请重新输入:'))
       
    else:
        return(heads, legs)

heads = float(input('请输入头的个数:'))
legs = float(input('请输入脚的个数:'))
(true_heads, true_legs) = number(heads, legs)
(chicken_number, rabbit_number) = question(true_heads, true_legs)
print('鸡的个数是%d, 兔的个数是%d' % (int(chicken_number), int(rabbit_number)))

请输入头的个数:34.5
请输入脚的个数:94
头的参数错误T_T!请输入一个正整数!
请重新输入头的个数:35
Traceback (most recent call last):
  File "C:/Users/Documents/学习/python/作业簿/鸡兔同笼.py", line 27, in <module>
    (true_heads, true_legs) = number(heads, legs)
TypeError: cannot unpack non-iterable NoneType object
最佳答案
2020-3-25 19:47:04
几个错误,解释看代码里的注释:
def question(heads, legs):
    rabbit_number_max = legs // 4
    rabbit_number = 0
    while True:
        chicken_number = (legs - rabbit_number * 4) / 2
        if chicken_number == int(chicken_number) and rabbit_number + chicken_number == heads:
            return (chicken_number, rabbit_number)
        elif rabbit_number <= rabbit_number_max:
            rabbit_number += 1
        else:
            print('题目错误啦*_*!此题无解!')
            break           #无解就无限循环了,至少加个break吧,然后默认返回None


def number(heads, legs):
    if heads <= 0 or heads != int(heads):
        print('头的参数错误T_T!请输入一个正整数!')
        heads = float(input('请重新输入头的个数:'))
#没有返回呀,默认返回None       
    elif legs <= 0 or legs != int(legs):
        print('脚的参数错误T_T!请输入一个正整数!')
        legs = float(input('请重新输入:'))
#同上       
#    else:  这样改就有正常的返回了
    return(heads, legs)

heads = float(input('请输入头的个数:'))
legs = float(input('请输入脚的个数:'))
(true_heads, true_legs) = number(heads, legs)
if question(true_heads, true_legs)!=None:   #加个判断,不然无解还运行下面语句就报错
    (chicken_number, rabbit_number) = question(true_heads, true_legs)
    print('鸡的个数是%d, 兔的个数是%d' % (int(chicken_number), int(rabbit_number)))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-25 19:38:59 | 显示全部楼层
def question(heads, legs):
    rabbit_number_max = legs // 4
    rabbit_number = 0
    while True:
        chicken_number = (legs - rabbit_number * 4) / 2
        if chicken_number == int(chicken_number) and rabbit_number + chicken_number == heads:
            return (chicken_number, rabbit_number)
        elif rabbit_number <= rabbit_number_max:
            rabbit_number += 1
        else:
            print('题目错误啦*_*!此题无解!')

def number(heads, legs):
    if heads <= 0 or heads != int(heads):
        print('头的参数错误T_T!请输入一个正整数!')
        heads = float(input('请重新输入头的个数:'))
       
    elif legs <= 0 or legs != int(legs):
        print('脚的参数错误T_T!请输入一个正整数!')
        legs = float(input('请重新输入:'))
       
    return(heads, legs)

heads = float(input('请输入头的个数:'))
legs = float(input('请输入脚的个数:'))
(true_heads, true_legs) = number(heads, legs)
(chicken_number, rabbit_number) = question(true_heads, true_legs)
print('鸡的个数是%d, 兔的个数是%d' % (int(chicken_number), int(rabbit_number)))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 19:47:04 | 显示全部楼层    本楼为最佳答案   
几个错误,解释看代码里的注释:
def question(heads, legs):
    rabbit_number_max = legs // 4
    rabbit_number = 0
    while True:
        chicken_number = (legs - rabbit_number * 4) / 2
        if chicken_number == int(chicken_number) and rabbit_number + chicken_number == heads:
            return (chicken_number, rabbit_number)
        elif rabbit_number <= rabbit_number_max:
            rabbit_number += 1
        else:
            print('题目错误啦*_*!此题无解!')
            break           #无解就无限循环了,至少加个break吧,然后默认返回None


def number(heads, legs):
    if heads <= 0 or heads != int(heads):
        print('头的参数错误T_T!请输入一个正整数!')
        heads = float(input('请重新输入头的个数:'))
#没有返回呀,默认返回None       
    elif legs <= 0 or legs != int(legs):
        print('脚的参数错误T_T!请输入一个正整数!')
        legs = float(input('请重新输入:'))
#同上       
#    else:  这样改就有正常的返回了
    return(heads, legs)

heads = float(input('请输入头的个数:'))
legs = float(input('请输入脚的个数:'))
(true_heads, true_legs) = number(heads, legs)
if question(true_heads, true_legs)!=None:   #加个判断,不然无解还运行下面语句就报错
    (chicken_number, rabbit_number) = question(true_heads, true_legs)
    print('鸡的个数是%d, 兔的个数是%d' % (int(chicken_number), int(rabbit_number)))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-25 19:48:00 | 显示全部楼层
同意楼上两位的代码。(至少可以顺利执行)
出现错误的原因是当遇到其他情况时number函数并不会返回一个元组,这样就没有办法再把参数传入question函数。
在你的实验中,输出了
头的参数错误T_T!请输入一个正整数!
之后,会直接执行下一行代码
(chicken_number, rabbit_number) = question(true_heads, true_legs)
因此return需要在任何条件都实现才可以继续进行。除了楼上的解决方式,你还可以:
1.在number函数中添加一个while循环以保证输入正确时才会跳出函数
2.在执行代码区使用try except语句避免报错

希望对你有所帮助
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-25 11:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表