2012-02-13 84 views
1

我正在编写一个模拟彩票游戏的程序,并且我被困在某个点。我试图将用户的猜测与获奖机票上的数字相匹配,但我的函数“checkmatch”显然需要2个参数,但我只给它1个?我知道网站上有类似的问题,但我是一个非常新手的程序员,而其他人似乎......比我高一点。这是我的全部程序(迄今为止):函数需要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) 

谢谢任何​​和所有的帮助!

+0

该错误是排序言自明的; 'checkmatch'需要参数'winning_numbers'和'guess',并且你只是通过'guess'传递它。 – geoffspear 2012-02-13 03:51:26

回答

3

问题是

nummatches = checkmatch(guess) 

在您的代码checkmatch需要两个参数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) 
>>> 
+0

非常感谢! – 2012-02-13 03:57:23

2

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

nummatches = checkmatch(guess) 

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

2

在为checkmatch你的函数定义,您明确告诉Python有两个参数,只要调用此函数:

def checkmatch(winning_numbers, guess): 
    ... 

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

nummatches = checkmatch(guess) 

既然你不是公关将wins_numbers参数排在checkmatch之后,就会出现错误。

看来你是这么做的,因为你已经在你的主程序的主体中使用了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的命名空间和全局VS局部变量,以及何时使用每个。另外,除非是非常特殊的情况,否则通常不是一个好主意。您已将变量指定为空列表,您的代码上下文中不需要再次手动删除其内容。

相关问题