鱼C论坛

 找回密码
 立即注册
查看: 820|回复: 1

[已解决]来自33讲课后习题的问题求助

[复制链接]
发表于 2018-9-27 04:08:53 | 显示全部楼层 |阅读模式

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

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

x
import random
def int_input():
    temp=input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
    try:
        return int(temp)
    except ValueError:
        int_input()

secret = random.randint(1,10)
print('------------------我爱鱼C工作室------------------')

guess = int_input()
if guess>secret:
    print(1)


为什么输入字符串错误后,int_input()循环,然后输入正确整数,到guess>secret这里就会报错 ,好像是secret就没被赋值了,求助

这是错误类型
TypeError: '>' not supported between instances of 'NoneType' and 'int'
最佳答案
2018-9-27 07:20:49
本帖最后由 塔利班 于 2018-9-27 08:07 编辑

你的函数异常没返回值,最后加上return
  1. import random
  2. def int_input():
  3.     temp=input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
  4.     try:
  5.         return int(temp)
  6.     except ValueError:
  7.         return int_input()

  8. secret = random.randint(1,10)
  9. print('------------------我爱鱼C工作室------------------')

  10. guess = int_input()
  11. if guess>secret:
  12.     print(1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-9-27 07:20:49 | 显示全部楼层    本楼为最佳答案   
本帖最后由 塔利班 于 2018-9-27 08:07 编辑

你的函数异常没返回值,最后加上return
  1. import random
  2. def int_input():
  3.     temp=input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
  4.     try:
  5.         return int(temp)
  6.     except ValueError:
  7.         return int_input()

  8. secret = random.randint(1,10)
  9. print('------------------我爱鱼C工作室------------------')

  10. guess = int_input()
  11. if guess>secret:
  12.     print(1)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-9-14 03:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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