Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python定义和调用函数-方法行为在处理用户输入时发生变化_Python_User Input - Fatal编程技术网

Python定义和调用函数-方法行为在处理用户输入时发生变化

Python定义和调用函数-方法行为在处理用户输入时发生变化,python,user-input,Python,User Input,我定义了一个方法,如下所示: class MyDatastructure(object): # init method here def appending(self, elem): self.data.append(elem) if self.count >= self.size: print "popping " + str(self.data[0]) print "inserting "

我定义了一个方法,如下所示:

class MyDatastructure(object):
    # init method here

    def appending(self, elem):
        self.data.append(elem)
        if self.count >= self.size:
            print "popping " + str(self.data[0])
            print "inserting " + str(elem)
            self.data.pop(0)
        elif self.count < self.size:
            self.count += 1
            print "count after end of method " + str(self.count)

这一行的问题在于:

x = raw_input()
调用
raw\u input
将返回一个字符串。如果我输入数字3,这意味着数据结构将把字符串
“3”
分配给大小

尝试将字符串与数字进行比较被认为是未定义的行为,并且会做一些奇怪的事情——请参见此。请注意,Python3修复了这一奇怪之处——尝试将字符串与int进行比较将导致出现
TypeError
异常

相反,您希望将其转换为int,以便正确地进行大小比较

x = int(raw_input())

您是如何获得用户输入的?通过原始输入,请参见我编辑的问题。如果某个条件,使用具有完全相反条件的elif进行后续操作是愚蠢的。它的功能与其他功能相同,但看起来有些奇怪。我知道这一点。我这样做是为了测试,因为我不明白为什么它没有输入if(最初我有if和else)!成功了。仍然需要习惯用Python键入。谢谢你提供了翔实的答案!
x = int(raw_input())