Python 在智囊团游戏中重复一个函数的问题

Python 在智囊团游戏中重复一个函数的问题,python,Python,我正在设计一个用python玩的智囊团游戏。但是,当我试图设置一个函数,使其在尝试不完全正确的情况下重复时,会遇到一些问题 我的代码分为两部分。对于第一部分,它要求用户输入正确的号码,然后第二个用户尝试输入他的尝试号码。代码的第二部分将他的尝试分解为数字列表,并计算正确的整数数和正确位置的整数数,然后如果答案不完全正确,程序会要求用户进行第二次输入 def getnumber(): predestine = input("Please input your test number") a = s

我正在设计一个用python玩的智囊团游戏。但是,当我试图设置一个函数,使其在尝试不完全正确的情况下重复时,会遇到一些问题

我的代码分为两部分。对于第一部分,它要求用户输入正确的号码,然后第二个用户尝试输入他的尝试号码。代码的第二部分将他的尝试分解为数字列表,并计算正确的整数数和正确位置的整数数,然后如果答案不完全正确,程序会要求用户进行第二次输入

def getnumber():
predestine = input("Please input your test number")
a = str(predestine)
attempt()

def attempt():
  attempt = input("Attempt:")
  b = str(attempt)
  correctrecord = []
  sequencerecord = []
  for i in b:
      if i in a:
          correctrecord.append(1)
  for i in range(0,4):
      if b[i] == a[i]:
        s  equencerecord.append(1)

  correctlength = len(correctrecord)
  sequencelength = len(sequencerecord)

  print(f"You have made {correctlength} correct attempts, and of these {sequencelength} are of correct positions")

  if sequencelength == 4:
      print("You have won, the game is ended")
  else:
      return attempt()

问题在于最后一个代码:return-trument()。它似乎无法重复该函数,并出现“str object not callable”错误

代码中的问题在于变量阴影

重复函数位于名为
trunt
的全局变量中。然后,在
trunt
函数中定义一个
trunt
字符串变量,该变量是该函数的局部变量,因此暂时隐藏保存该函数的全局
trunt
变量

因此,调用
trunt()
失败,因为您实际上是在尝试调用字符串

解决方案是重命名局部字符串变量
trunt
,使其不覆盖全局变量:

def attempt():
    attempt_ = input("Attempt:")
    b = str(attempt_)
    correctrecord = []
    sequencerecord = []
    for i in b:
        if i in a:
            correctrecord.append(1)
    for i in range(0,4):
        if b[i] == a[i]:
            sequencerecord.append(1)

    correctlength = len(correctrecord)
    sequencelength = len(sequencerecord)

    print(f"You have made {correctlength} correct attempts, and of these {sequencelength} are of correct positions")

    if sequencelength == 4:
        print("You have won, the game is ended")
    else:
        return attempt()

您可以多次使用相同的变量名。Python函数是一流的公民,它允许您执行以下操作:

# define a function by name r
def r():
    return 1

print(type(r))  # <class 'function'>     now r is a function

# reassign name r to be a string
r = "22"
print(type(r))  # <class 'str'>          now r is a string
输出:

Please input your test number: 1234
Attempt: 1111
You have found 1 correct digits, and of these 1 are of correct positions.
Attempt: 1212
You have found 2 correct digits, and of these 2 are of correct positions.
Attempt: 1321
You have found 3 correct digits, and of these 1 are of correct positions.
Attempt: 1234
You have found 4 correct digits, and of these 4 are of correct positions.
You have won, the game is ended
Please input your test number: 1234
Attempt: 1111
You have found 1 correct digits, and of these 1 are of correct positions.
Attempt: 1212
You have found 2 correct digits, and of these 2 are of correct positions.
Attempt: 1321
You have found 3 correct digits, and of these 1 are of correct positions.
Attempt: 1234
You have found 4 correct digits, and of these 4 are of correct positions.
You have won, the game is ended