我正在编写一个模拟彩票游戏的程序,但我卡在了某个点上。我试图将用户的猜测与中奖彩票上的数字相匹配,但我的函数“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)
感谢您的所有帮助!
请您参考如下方法:
问题是
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)
>>>
