简体   繁体   English

Python中的功能列表以多个对象读取

[英]List in Function in Python Read as Multiple Objects

I have a function that looks like this: 我有一个看起来像这样的函数:

class Question:
    Ans = "";
    checkAns = 0;
    isPos=False;
    def Ask(Q, PosAns):
        checkAns=0;
        Ans = raw_input("{} ({})".format(Q,PosAns));
        while(checkAns<len(PosAns) and isPos!=True):
            if(Ans==PosAns[x]):
                isPos=True; #if it IS a possible answer, the loop ends
            checkAns+=1;
            if(checkAns==len(PosAns)):
#If the loop goes through all the possible answers and still doesn't find a
#match, it asks again and resets checkAns to zero.
                Ans = raw_input("{} ({})".format(Q,PosAns));
                checkAns=0;
        return ("Good Answer");

ques = Question();
print(ques.Ask("Do you like to code?",["Yes","No"]));

First off, the point of this function is to take in a question (Q) and all the possible answers (PosAns), and if the user puts in something that is not one of the possible answers, then the function will simply ask again. 首先,此功能的重点是输入问题(Q)和所有可能的答案(PosAns),如果用户输入的内容不是可能的答案之一,则该功能将再次询问。

Every time I run it, however, it says that the Ask() function can only handle two parameters and that I've given it three (note that YesNo has two strings inside). 但是,每次运行它时,它都会说Ask()函数只能处理两个参数,而我给了它三个(请注意YesNo里面有两个字符串)。 Why does it read the list's objects instead of taking the list as a parameter? 为什么它读取列表的对象而不是将列表作为参数? How can I make it take the list as the parameter? 如何使列表作为参数?

I do recognize that the way I code is roundabout and strange to most people, but it's just the way things make sense to me. 我确实认识到我的编码方式对大多数人来说都是round回旋且陌生,但这只是我觉得有意义的方式。 I'm more interested in the answer to my question than a new way to write the whole function (I'm still working on it). 我对问题的答案比对编写整个函数的新方法更感兴趣(我仍在研究中)。

You missed the 'self' in method declaration. 您错过了方法声明中的“自我”。 Every class method (except static methods) require first argument to be self . 每个类方法(静态方法除外)都要求第一个参数为self self is implicily passed so doesnt show up in our method calls. 隐式传递了self,因此不会出现在我们的方法调用中。 self can be used to refer the the other attributes of call like isPos , checkAns and ans variables in this example 在此示例中,self可以用于引用调用的其他属性,例如isPoscheckAnsans变量

Though one thins I wann't able to figure out what is x here if (Ans == PosAns[x]) 虽然我很瘦,但if (Ans == PosAns[x])我将无法找出x是什么。

class Question:
Ans = "";
checkAns = 0;
isPos = False;

def Ask(self, Q, PosAns):
    Ans = raw_input("{} ({})".format(Q, PosAns));
    while (self.checkAns < len(PosAns) and self.isPos != True):
        if (Ans == PosAns[x]):
            isPos = True;  # if it IS a possible answer, the loop ends
        self.checkAns += 1;
        if (self.checkAns == len(PosAns)):
            # If the loop goes through all the possible answers and still doesn't find a
            # match, it asks again and resets checkAns to zero.
            self.Ans = raw_input("{} ({})".format(Q, PosAns));
            self.checkAns = 0;
    return ("Good Answer");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM