I'm writing a program that simulates a lottery game, and I'm stuck at a certain point. I'm trying to match the user's guesses with the numbers on the winning ticket, but my function "checkmatch" apparently takes 2 arguments, but I'm only giving it 1? I know there are similar questions on the site, but I'm a very novice programmer, and the others seemed... a bit above me. This is my program in its entirety (thus far):
我正在编写一个模拟彩票游戏的程序,我被困在了某个点上。我试图将用户的猜测与获胜票上的数字匹配,但我的函数“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
And this is the part that is giving me trouble:
这部分给我带来了麻烦
def checkmatch(winning_numbers, guess):
match = 0
for i in range(5):
if winning_numbers[i] == guess[i]:
match = match+1
return match
And here is what I get when I try a test run:
这是我试着运行测试时得到的结果:
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)
Thank you for any and all help!
谢谢你的帮助!
3 个解决方案
#1
3
The problem is
现在的问题是
nummatches = checkmatch(guess)
In your code checkmatch
takes 2 arguments winning_numbers & guess
but when you are calling it you are only giving a single argument.
在你的代码检查中,有两个参数winning_numbers和guess,但是当你调用它时,你只给出一个参数。
Like for example
比如
>>> 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
2
The problem isn't in the function definition. It's here:
问题不在于函数定义。在这里:
nummatches = checkmatch(guess)
In the error statement, you'll notice that there are line numbers. The line causing the error is at the very bottom of the error statement. The problem is on line 36; if you have a text editor that shows line numbers, you'll find it easer to figure out where the error is.
在error语句中,您将注意到有行号。导致错误的行位于错误语句的最底部。问题在第36行;如果有一个显示行号的文本编辑器,您会发现找出错误在哪里更容易。
#3
2
In your function definition for checkmatch, you explicitly tell Python to expect two arguments whenever this function is called:
在您的checkmatch函数定义中,您显式地告诉Python,每当调用该函数时,都需要两个参数:
def checkmatch(winning_numbers, guess):
...
In the body of your program, however, you call it with only one argument:
然而,在你的程序主体中,你只需要一个参数就可以调用它:
nummatches = checkmatch(guess)
Since you are not providing the winning_numbers argument to checkmatch, you get the error.
由于没有为checkmatch提供winning_numbers参数,因此会得到错误。
It appears that you are doing this because you have already used winning_numbers in the body of your main program. This is where the actual mistake is, you are presuming that because the variable in the body of your program has the same name as the variable in your function definition that the variable winning_numbers is automatically being passed in.
看起来您这样做是因为您已经在主程序的主体中使用了winning_numbers。这就是真正的错误所在,因为程序主体中的变量与函数定义中的变量具有相同的名称,因此winning_numbers变量将自动传入。
The winning_numbers argument in the function definition is a local variable to the function checkmatch, it merely tells Python to expect a value to be given by the user in that position when the function is called, and then allows that name to be used to represent that value inside the function definition itself. The winning_numbers list you have in your main program, however, is an example of a global variable and since your function definition reuses that exact same name you're giving Python illogical directions to follow, hence the error.
winning_numbers参数在函数定义一个局部变量,函数checkmatch,它只是告诉Python期望一个值是由用户在那个位置函数被调用,然后将这个名字用于表示该值在函数定义本身。然而,您在主程序中拥有的winning_numbers列表是一个全局变量的示例,由于函数定义重用了您要遵循的与Python不符合逻辑的方向相同的名称,因此出现了错误。
To fix, either a) pass in the variable explicitly:
a)明确传递变量:
nummatches = checkmatch(winning_numbers, guess)
... or b) properly use the global variable.
…或b)正确使用全局变量。
def checkmatch(guess):
global winning_numbers
...
I would suggest, however, that you're best off reading up more on Python namespaces and global vs local variables, and when to use each.
不过,我建议您最好多了解Python名称空间和全局变量vs本地变量,以及何时使用它们。
Also, as an aside, it is generally not a good idea to use the del builtin except in very specific cases. You have already assigned your variables to be empty lists, there's no need in your code's context to manually delete their contents again.
此外,顺便说一句,除了非常特殊的情况外,使用del builtin通常不是一个好主意。您已经将变量分配为空列表,在代码的上下文中不需要再次手动删除它们的内容。
#1
3
The problem is
现在的问题是
nummatches = checkmatch(guess)
In your code checkmatch
takes 2 arguments winning_numbers & guess
but when you are calling it you are only giving a single argument.
在你的代码检查中,有两个参数winning_numbers和guess,但是当你调用它时,你只给出一个参数。
Like for example
比如
>>> 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
2
The problem isn't in the function definition. It's here:
问题不在于函数定义。在这里:
nummatches = checkmatch(guess)
In the error statement, you'll notice that there are line numbers. The line causing the error is at the very bottom of the error statement. The problem is on line 36; if you have a text editor that shows line numbers, you'll find it easer to figure out where the error is.
在error语句中,您将注意到有行号。导致错误的行位于错误语句的最底部。问题在第36行;如果有一个显示行号的文本编辑器,您会发现找出错误在哪里更容易。
#3
2
In your function definition for checkmatch, you explicitly tell Python to expect two arguments whenever this function is called:
在您的checkmatch函数定义中,您显式地告诉Python,每当调用该函数时,都需要两个参数:
def checkmatch(winning_numbers, guess):
...
In the body of your program, however, you call it with only one argument:
然而,在你的程序主体中,你只需要一个参数就可以调用它:
nummatches = checkmatch(guess)
Since you are not providing the winning_numbers argument to checkmatch, you get the error.
由于没有为checkmatch提供winning_numbers参数,因此会得到错误。
It appears that you are doing this because you have already used winning_numbers in the body of your main program. This is where the actual mistake is, you are presuming that because the variable in the body of your program has the same name as the variable in your function definition that the variable winning_numbers is automatically being passed in.
看起来您这样做是因为您已经在主程序的主体中使用了winning_numbers。这就是真正的错误所在,因为程序主体中的变量与函数定义中的变量具有相同的名称,因此winning_numbers变量将自动传入。
The winning_numbers argument in the function definition is a local variable to the function checkmatch, it merely tells Python to expect a value to be given by the user in that position when the function is called, and then allows that name to be used to represent that value inside the function definition itself. The winning_numbers list you have in your main program, however, is an example of a global variable and since your function definition reuses that exact same name you're giving Python illogical directions to follow, hence the error.
winning_numbers参数在函数定义一个局部变量,函数checkmatch,它只是告诉Python期望一个值是由用户在那个位置函数被调用,然后将这个名字用于表示该值在函数定义本身。然而,您在主程序中拥有的winning_numbers列表是一个全局变量的示例,由于函数定义重用了您要遵循的与Python不符合逻辑的方向相同的名称,因此出现了错误。
To fix, either a) pass in the variable explicitly:
a)明确传递变量:
nummatches = checkmatch(winning_numbers, guess)
... or b) properly use the global variable.
…或b)正确使用全局变量。
def checkmatch(guess):
global winning_numbers
...
I would suggest, however, that you're best off reading up more on Python namespaces and global vs local variables, and when to use each.
不过,我建议您最好多了解Python名称空间和全局变量vs本地变量,以及何时使用它们。
Also, as an aside, it is generally not a good idea to use the del builtin except in very specific cases. You have already assigned your variables to be empty lists, there's no need in your code's context to manually delete their contents again.
此外,顺便说一句,除了非常特殊的情况外,使用del builtin通常不是一个好主意。您已经将变量分配为空列表,在代码的上下文中不需要再次手动删除它们的内容。