函数只需2个参数(给定1个)

时间:2012-02-13 03:48:18

标签: python function typeerror

我正在编写一个模拟彩票游戏的程序,而且我已经陷入了困境。我试图将用户的猜测与中奖彩票上的数字相匹配,但是我的功能" 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)

感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

问题是

nummatches = checkmatch(guess)

在你的代码中checkmatch需要2个参数winning_numbers & 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)
>>>

答案 1 :(得分:2)

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

nummatches = checkmatch(guess)

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

答案 2 :(得分:2)

在checkmatch的函数定义中,无论何时调用此函数,都明确告诉Python期望两个参数:

def checkmatch(winning_numbers, guess):
   ...

但是,在程序正文中,只用一个参数调用它:

nummatches = checkmatch(guess)

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

看来你这样做是因为你已经在主程序的主体中使用了wins_numbers。这是实际错误的地方,你假设因为程序体中的变量与函数定义中的变量同名,变量winsnumbers会自动传入。

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

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

nummatches = checkmatch(winning_numbers, guess)

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

def checkmatch(guess):
    global winning_numbers
    ...

但是,我建议您最好阅读有关Python名称空间和全局变量以及局部变量的更多信息,以及何时使用每个变量。

另外,除非在特定情况下,否则使用 del 内置通常不是一个好主意。您已将变量指定为空列表,您的代码上下文中无需再次手动删除其内容。