函数需要正好2个参数(提供了1个)

1 投票
3 回答
23685 浏览
提问于 2025-04-17 12:55

我正在写一个模拟彩票游戏的程序,但在某个地方卡住了。我想把用户的猜测和中奖号码进行匹配,但我的“checkmatch”函数似乎需要两个参数,而我只给了它一个?我知道网站上有类似的问题,但我还是个新手程序员,其他的回答对我来说有点难。这是我目前的完整程序:

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 个回答

2

问题不在于函数的定义,而是在这里:

nummatches = checkmatch(guess)

在错误提示中,你会看到有行号。导致错误的那一行就在错误提示的最底部。问题出在第36行;如果你使用的文本编辑器能显示行号,那么你会更容易找到错误所在。

2

在你定义的checkmatch函数里,你明确告诉Python这个函数在被调用时需要两个参数:

def checkmatch(winning_numbers, guess):
   ...

但是在你的程序中,你只用一个参数来调用它:

nummatches = checkmatch(guess)

因为你没有给checkmatch提供winning_numbers这个参数,所以就出现了错误。

看起来你这样做是因为在主程序的部分你已经使用了winning_numbers。这里的实际错误在于,你假设因为程序中的变量和函数定义中的变量同名,所以winning_numbers会自动传入。

函数定义中的winning_numbers参数是一个局部变量,它只是告诉Python在调用这个函数时,用户需要在这个位置提供一个值,然后在函数内部可以用这个名字来代表这个值。然而,你在主程序中使用的winning_numbers列表是一个全局变量,因为你的函数定义重用了这个名字,导致Python无法理解你的意图,从而出现了错误。

要解决这个问题,你可以选择:a) 明确地传入变量:

nummatches = checkmatch(winning_numbers, guess)

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

def checkmatch(guess):
    global winning_numbers
    ...

不过,我建议你多了解一下Python的命名空间,以及全局变量和局部变量的区别,以及什么时候使用它们。

另外,顺便提一下,通常不建议使用del这个内置函数,除非在非常特定的情况下。你已经把变量赋值为空列表,在你的代码上下文中没有必要再手动删除它们的内容。

3

问题是

nummatches = checkmatch(guess)

在你的代码中,checkmatch这个函数需要两个参数,分别是winning_numbersguess,但是你在调用这个函数的时候,只传递了一个参数。

比如说

>>> 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)
>>>

撰写回答