类型错误(不可排序的类型:int() <= NoneType()) [英] TypeError (unorderable types: int() &lt;= NoneType())

查看:24
本文介绍了类型错误(不可排序的类型:int() <= NoneType())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次用 Python 编写代码,可能需要一些帮助.我正在使用 Python 34,根本无法理解发生了什么.

This is my first time writing code in Python and could use some help. I am using Python 34 and simply cannot understand what is going on.

def roll(v):
    x = input()
    return (x + v)

def startGame():
    v = 0
    while 0 <= v: # error points to this line
        v = roll(v)  

print("Thanks for playing")

我声明 v 是一个值为 0 的整数.但是当我尝试将它与另一个整数进行比较时,它给了我错误消息 unorderable types: int() <= NoneType()我可以使用一些指导..谢谢

I declare v to be an integer with a value of 0. But when I try to compare it to another integer, it gives me the error message unorderable types: int() <= NoneType() I could use some guidance.. Thanks

推荐答案

我将您发布的代码放入 python 3 shell,但它在其他地方失败.你的 x = input() 产生一个字符串,python 不知道如何将字符串转换为数字,除非你明确告诉它如何.

I placed the code you posted into a python 3 shell but it fails somewhere else. Your x = input() produces a string, and python doesn't know how to convert strings to numbers unless you explicitly tell it how.

所以:

def roll(v)
    # Lets try to parse userinput
    try:
        x = int(input())
    # sometimes users don't get it that "a" is no int
    except ValueError:
        x = 0
    return (x+v)

def roll(v)
    hasProducedNumber = False
    x = 0
    # we keep nagging for a number till no valueerror arises
    while not hasProducedNumber:
        try:
            x = int(input())
            hasProducedNumber = True
        except ValueError:
            print("Please provide a number")

    return (x + v)

如果这不是您问题的解决方案,我需要实际产生您的错误的相关代码:)

If this is not a solution to your problem I need relevant code that actually produces your error :)

这篇关于类型错误(不可排序的类型:int() <= NoneType())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆