Python报错TypeError: '>='不支持NoneType与int比较的问题求助
秒转分钟Python代码的TypeError问题解决
错误根源
你代码里的核心问题是这一行:
secondsInput = print(float(input('Input any number of seconds: ')))
print()函数的返回值是None,所以secondsInput被赋值成了NoneType,后续用它和整数secInmin做比较时,自然会触发TypeError: '>=' not supported between instances of 'NoneType' and 'int'。
另外还有两个小问题:
print(float('you have', userSec, 'minutes in your seconds'))写法错误,float()不能接收多个字符串参数,而且不需要把print的内容转成float。- 第二个
if分支的输出逻辑错误,你原本想表达“输入的秒数不足1分钟”,但代码里输出的是secInmin(即60),逻辑不符。
修正后的代码
secInmin = 60 secInhour = 3600 secIndays = 86400 # 先获取输入并转换为浮点数,单独赋值 secondsInput = float(input('Input any number of seconds: ')) # 可选:打印输入值,和赋值操作分开 print(f"你输入的秒数是: {secondsInput}") if secondsInput >= secInmin: userSec = secondsInput / secInmin print(f"你的秒数换算成分钟是: {userSec}") elif secondsInput < secInmin: print(f"你的秒数不足1分钟,共 {secondsInput} 秒")
关键修正点
- 把输入获取和打印操作分开:先将输入转换为浮点数并赋值给
secondsInput,再按需单独打印输入值。 - 用f-string格式化输出,更简洁清晰,避免类型转换错误。
- 把第二个独立的
if改成elif,逻辑更严谨,避免不必要的重复判断。
内容的提问来源于stack exchange,提问作者Thundoisking
相关产品推荐
相关产品推荐

