-3

if matches in ticket我的函数在该点之后立即停止工作,matches将错误列为无效输入。这是什么原因,我该如何解决?我对 Python 很陌生,所以非常感谢任何帮助。此外,此功能仍在进行中,我认识到它目前的效率非常低,但我正在尝试在更改其他任何内容之前修复此错误。


import random

def matches(ticket, winner): 
    score = 0
    attemptnum = 0
    for number in ticket:
    if number in winner:
        score += 1
    return score
def winner():
  win1 == rand.randint(1, 69)
  win2 == rand.randint(1, 69)
  win3 == rand.randint(1, 69)
  win4 == rand.randint(1, 69)
  win5 == rand.randint(1, 69)
  win6 == rand.randint(1, 69)
  winner == 'win1 win2 win3 win4 win5 win6'
def playPowerBall():
    print('Please select your first unique number between 1-69.')
    def ticket():
      num1 = int(input('1st Number:'))
      if num1 in winner():
        score += 1
        matches += 1
      else:
        score += 0 
        matches += 0
      num2 = int(input('2nd Number:'))
      if num2 in winner():
        score += 1
        matches += 1
      else:
        score += 0 
        matches += 0
      num3 = int(input('3rd Number:'))
       if num3 in winner():
        score += 1
        matches += 1
      else:
        score += 0 
        matches += 0
      num4 = int(input('4th Number:'))
      if num4 in winner():
        score += 1
        matches += 1
      else:
        score += 0 
        matches += 0
      num5 = int(input('5th Number:'))
      if num5 in winner():
        score += 1
        matches += 1
      else:
        score += 0 
        matches += 0
      num6 = int(input('6th Number:'))
      if num6 in winner():
        score += 1
        matches += 1
      else:
        score += 0 
        matches += 0
      attempts = num1, num2, num3, num4, num5, num6
    for matches in ticket:
      score += 1
    else:
      score += 0      
    if matches in ticket:
      print('You picked at least ' matches 'winning numbers in 6 attempts, please claim your cash prize.')
    else: 
      print('You do not have a winning combination. Would you like to play Powerball again?')
      response = str(raw_input('Y/N:')):
      if response == 'Y':
        os.execl(sys.executable, sys.executable, *sys.argv)
      else:
        print('Thank you for playing.')
        sys.exit(0)
4

1 回答 1

1

在阅读您的代码时,我正在接受一些事情:

  1. 你的缩进仍然无处不在,有些地方你使用 2 个空格,有些地方你使用 4 个 - 你需要解决这个问题,python 严重依赖缩进来编译。
  2. 在你的函数winner中你什么都不做,双等号是一个测试运算符,不会做任何赋值,它只会检查win1 中的值是否等于随机生成的数字。
  3. 在同一个函数中,您创建了一个winner包含字符串的变量,其中包含字符win1 win2 ...,我只能假设您正在尝试创建一个包含这些 win1 变量的对象,因为您可以使用list,如下所示: winners = [win1, win2, win3, win4, win5, win6]
  4. 在同一个函数中,您没有返回任何内容,因此该函数是无用的,为了将函数的输出发送回调用它的对象,您必须使用return(winners)

我不知道你在最大的playPowerBall函数中要做什么,所以我只讨论一些明显让你感到困惑的事情:

  1. def作为关键字定义了一个函数,但不执行它,您必须调用该函数才能运行它。
  2. 就像我之前说的,为了从函数中发送结果,您必须返回答案/结果。
  3. 我有一种预感,而不是看到invalid input您看到invalid syntax错误,因为您正在尝试检查整数matches是否在函数指针内ticket。这不是有效的语法,要进行函数调用,您必须使用括号,否则 python 认为您指的是函数指针,因此matches in ticket()可能是有效的语法,但是正如您定义的那样,我看不出它是如何工作的。

这段代码是否曾经正确编译或运行过?我认为在尝试构建如此复杂的东西之前,您需要回到关于函数定义的更多基本内容,从一个函数定义开始,尝试让它在一个小程序中工作,然后再回到这个。

如果还有什么我可以帮忙的,请告诉我。

于 2018-12-05T10:46:40.320 回答