【问题标题】:Function takes exactly 2 arguments (1 given)函数只需要 2 个参数(给定 1 个)
【发布时间】:2012-03-04 13:11:43
【问题描述】:

我正在编写一个模拟彩票游戏的程序,但我卡在了某个点上。我试图将用户的猜测与中奖彩票上的数字相匹配,但我的函数“checkmatch”显然需要 2 个参数,但我只给它 1?我知道网站上也有类似的问题,但我是一个非常新手的程序员,其他人似乎……比我略胜一筹。这是我的全部程序(到目前为止):

import random

def main():
random.seed()

#Prompts the user to enter the number of tickets they wish to play.
tickets = int(input("How many lottery tickets do you want?\n"))

#Creates the dictionaries "winning_numbers" and "guess"
winning_numbers = []
guess = []

#Generates the winning lotto numbers.
for i in range(tickets):
    del winning_numbers[:]
    del guess[:]
    a = random.randint(1,30)
    winning_numbers.append(a)

    b = random.randint(1,30)
    while not (b in winning_numbers):
        winning_numbers.append(b)


    c = random.randint(1,30)
    while not (c in winning_numbers):
        winning_numbers.append(c)


    d = random.randint(1,30)
    while not (d in winning_numbers):
        winning_numbers.append(d)

getguess(guess, tickets)
nummatches = checkmatch(guess)
nummisses = checkmiss()

    #print(winning_numbers)

#Gets the guess from the user.
def getguess(guess, tickets):
    del guess[:]

for i in range(tickets):
    bubble = input("What numbers do you want to choose for ticket #"+str(i+1)+"?\n").split(" ")
    guess.append(bubble)

#Checks the user's guesses with the winning numbers.
def checkmatch(winning_numbers, guess):
    match = 0
    for i in range(5):
        if winning_numbers[i] == guess[i]:
            match = match+1

return match

这是给我带来麻烦的部分:

def checkmatch(winning_numbers, guess):
match = 0
for i in range(5):
    if winning_numbers[i] == guess[i]:
        match = match+1

return match

这是我尝试运行测试时得到的结果:

How many lottery tickets do you want?
3
What numbers do you want to choose for ticket #1?
1 2 3 4 5
What numbers do you want to choose for ticket #2?
1 2 3 4 5
What numbers do you want to choose for ticket #3?
1 2 3 4 5 
Traceback (most recent call last):
  File "C:/Users/Ryan/Downloads/Program # 2/Program # 2/lottery.py", line 64, in    <module>
    main()
   File "C:/Users/Ryan/Downloads/Program # 2/Program # 2/lottery.py", line 36, in main
    checkmatch(guess)
TypeError: checkmatch() takes exactly 2 arguments (1 given)

感谢您的所有帮助!

【问题讨论】:

  • 这个错误是不言自明的; checkmatch 需要参数 winning_numbersguess,而你只是传递了 guess

标签: python function typeerror


【解决方案1】:

问题是

nummatches = checkmatch(guess)

在您的代码中 checkmatch 接受 2 个参数 winning_numbers &amp; guess 但当您调用它时,您只给出一个参数。

例如

>>> def myfunc(str1,str2):
...   print str1+str2
...
>>> myfunc('a','b') #takes 2 argument and concatenates
ab
>>> myfunc('a') # only one given so ERROR
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: myfunc() takes exactly 2 arguments (1 given)
>>>

【讨论】:

    【解决方案2】:

    问题不在函数定义中。在这里:

    nummatches = checkmatch(guess)
    

    在错误语句中,您会注意到有行号。导致错误的行位于错误语句的最底部。问题在第 36 行;如果您有一个显示行号的文本编辑器,您会发现更容易找出错误所在。

    【讨论】:

      【解决方案3】:

      在您的 checkmatch 函数定义中,您明确告诉 Python 在调用此函数时需要两个参数:

      def checkmatch(winning_numbers, guess):
         ...
      

      然而,在你的程序主体中,你只用一个参数调用它:

      nummatches = checkmatch(guess)
      

      由于您没有向 checkmatch 提供winning_numbers 参数,因此您会收到错误消息。

      您这样做似乎是因为您已经在主程序的主体中使用了winning_numbers。这就是真正的错误所在,您假设因为程序主体中的变量与函数定义中的变量同名,所以变量winning_numbers 被自动传入。

      函数定义中的winning_numbers参数是函数checkmatch的局部变量,它只是告诉Python在调用函数时期望用户在该位置给出一个值,并且然后允许该名称用于表示函数定义本身内的该值。但是,您在主程序中拥有的winning_numbers 列表是全局变量 的示例,并且由于您的函数定义重用了完全相同的名称,因此您给出了Python 不合逻辑的指导方向,因此出现了错误。

      要修复,a) 显式传入变量:

      nummatches = checkmatch(winning_numbers, guess)
      

      ... 或 b) 正确使用全局变量。

      def checkmatch(guess):
          global winning_numbers
          ...
      

      不过,我建议您最好阅读有关 Python 命名空间和全局变量与局部变量以及何时使用它们的更多信息。

      另外,顺便说一句,除了非常特殊的情况外,使用 del 内置函数通常不是一个好主意。您已经将变量分配为空列表,无需在代码上下文中再次手动删除它们的内容。

      【讨论】:

        猜你喜欢
        • 2014-09-04
        • 2012-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2018-08-20
        • 1970-01-01
        相关资源
        最近更新 更多