Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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_Typeerror - Fatal编程技术网

将伪代码转换为Python时出错

将伪代码转换为Python时出错,python,typeerror,Python,Typeerror,我得到了这个算法来转换为伪代码: Name[1] = 'Ben' Name[2] = 'Thor' Name[3] = 'Zoe' Name[4] = 'Kate' Max <- 4 Current <- 1 Found <- False OUTPUT 'What player are you looking for?' INPUT PlayerName WHILE (Found = False) AND (Current <= Max) IF Names[Cur

我得到了这个算法来转换为伪代码:

Name[1] = 'Ben'
Name[2] = 'Thor'
Name[3] = 'Zoe'
Name[4] = 'Kate'
Max <- 4
Current <- 1
Found <- False
OUTPUT 'What player are you looking for?'
INPUT PlayerName
WHILE (Found = False) AND (Current <= Max)
    IF Names[Current] = PlayerName
        THEN Found <- True
        ELSE Current <- Current + 1
    ENDIF
ENDWHILE
IF Found = True
    THEN OUTPUT 'Yes, they have a top score'
    ELSE OUTPUT 'No, they do not have a top score'
ENDIF

我哪里出错了

您将收到一个
类型
错误,因为您试图将参数传递给
名称
函数<代码>名称不接受任何参数,因为其中没有列出任何参数。在声明
Names
函数时,您需要调用不带参数的函数,或者在括号中添加一个参数。此外,函数的局部变量不能从外部访问,除非返回。所以不要这样做:

Names["Something"] == Another_Thing
但是,将需要函数输出的内容作为参数传递。如果需要全局访问其中的所有内容,不要在函数内部定义所有内容,而是在函数外部定义所有内容,并将尝试执行的所有内容作为参数传递给函数

def Names(A_Parameter, Another_Parameter, A_Default_Parameter = Some_Default):
    # do something

让我们分析一下您得到的错误:

'TypeError: Names() takes 0 positional arguments but 1 was given'
它说,您创建的名为
Names()
的函数接受零个参数,但您只给了它一个参数

在代码中,当您在函数
Names()
中使用
Names(当前)
时,可以看到这一点。实际上,这是再次调用该函数,因此您无意中编写了一个不需要的递归函数

您要做的是检查名称列表,查看是否有任何名称与用户输入的名称匹配

为此,使用for循环代替while循环。这样,您就不必特别说明列表的开始点和结束点,如果您愿意,还可以添加其他名称

这是您的转换应该是什么样子的:

def Names():
    names_list= ['Ben', 'Thor', 'Zoe', 'Kate']
    Found = False
    PlayerName = str(raw_input("What Player are you looking for? "))
    for name in names_list:
        if name == PlayerName:
            Found = True

    if Found == True:   #place this if-else outside your for-loop
        print("Yes, they have a top score.")
    else:
        print("No, they do not have a top score.")


Names()
这将产生:

>>>
What Player are you looking for? Thor
Yes, they have a top score.

>>>
What Player are you looking for? Bob
No, they do not have a top score.

@逻辑有一个解决方案,但可以简化。不需要
str
循环,或
找到
变量

def Names():
    names_list = ['Ben', 'Thor', 'Zoe', 'Kate']
    PlayerName = raw_input("What Player are you looking for? ")
    if PlayerName in names_list:
        print "Yes, they have a top score."
    else:
        print "No, they do not have a top score."

Names()
Python2.x中的
print
也不应该有括号。在上述情况下,这并不重要,但与以下情况有关:

PlayerName = "Ben"
print("Name:",PlayerName)
print "Name:",PlayerName
输出:

('Name:', 'Ben')
Name: Ben

您能给出返回给您的错误代码吗?是否尝试运行该函数?似乎在调用
Names()
函数时出错。你能给我们看一下那个部分吗?你把
Current
传递给你的函数
Names()
。但是
Names()
不是用来接受任何输入的。你似乎把
Names
函数与
Names
列表(我猜这应该是一个列表)混淆了。你能重命名你的变量吗?另外,我想你应该使用方括号,例如
Names[Current]
也不要使用变量名
list
使用
name\u list
或类似名称,事实上,您应该使用它而不是
name(Curent)
->
name\u list[当前]
一切都很好,谢谢您的解释。只有一个问题。当它确实输出答案时,它会输出每个名称的答案,但它必须只输出用户键入的名称的答案。@Filip您只需将if-else置于循环之外。请参阅Eddio.@ FILIP No.PROB:请考虑检查左边的答案,如果我们解决了您的问题,没有做代码>名称[“某事”] =另一个事物< /代码>。您是根据伪代码回答问题的,但他的错误是问题底部Python代码的结果。错误地调用了
名称(当前)
。这就是导致错误的原因。@logic我也列出了它。。。问题是缺少形式参数。“Python2.x中的打印也不应该有括号”——这是真的吗?或者更像是“Python2.x中的print不需要括号”?好吧-对于像我这样看不出区别的人,
print(“Name:,PlayerName)
打印一个包含两个元素的元组,而
print“Name:,PlayerName
执行您所期望的操作。谢谢你的澄清这太好了!:)我只是添加了for循环,因为给定的伪代码中有一个循环。我不想太彻底地修改它(OP实际上应该使用while循环)。@logic,是的,最初的算法考虑的是一种不那么简洁的语言:)
PlayerName = "Ben"
print("Name:",PlayerName)
print "Name:",PlayerName
('Name:', 'Ben')
Name: Ben