我如何将 'level one' 函数设置为 运行,因为它一直在崩溃?

how do i get the 'level one' function to run as it keeps crashing?

当程序为 运行 时,它通过菜单和播放功能,但在进入 'level one' 功能时崩溃,因为它指出存在名称错误。

def main():
   menu()

def menu():
   print()

   choice = input("""
                   A: Play
                   Q: Exit
                   Please enter your choice: """)

   if choice == "A" or choice =="a":
     play()
   elif choice=="Q" or choice=="q":
     sys.exit
   else:
     print("You must only select either A or Q")
     print("Please try again")

def play():
    choice = input("""
                   A: Level One
                   Q: Exit
                   Please enter your choice: """)

    if choice == "A" or choice =="a":
       levelOne()
    elif choice=="Q" or choice=="q":
       sys.exit
    else:
        print("You must only select either A or Q")
        print("Please try again")
menu()
main()

import random
array = []

for i in range(3):
  randomNumber = random.randint(0,100)
  array.append(randomNumber)

# this function displays the random number for 1250 milliseconds
def randomNumberDisplay():
  import tkinter as tk
  root = tk.Tk()
  root.title("info")
  tk.Label(root, text=array).pack()
  # time in ms
  root.after(2150, lambda: root.destroy())
  root.mainloop()   
randomNumberDisplay()

#this function requires the user to enter the numbers displayed.
score = 0
def levelOne():
  incorrect = 0
  for i in range (3):
     userNumber = int(input("please enter the numbers you saw IN ORDER(press Enter when finished): "))

  #if they enter the right number, they gain a score and get to move to the next level
  if userNumber != array:
      print ("the numbers where: ", array)
      incorrect = incorrect +1
      print("you got ", incorrect, "wrong")
  else:
      score += 100
      i = i + 1
      print ("you have ",score, "points")
levelOne()
play()

程序应该 运行 完成所有功能,包括 'levelone' 用户必须在其中输入显示的数字。但是,它崩溃说它没有定义。如何解决?

您需要先将第56行的levelOne()函数的定义移动到play()的第27行调用之前定义。您可能还想删除对方法的重复调用,因为 menu() 和 play() 等是在您的函数本身中调用的。

解释:

请注意,Python 解释器将在第 33 行到达 menu(),然后在 menu() 内部它将在第 13 行调用 play(),然后它将尝试在第 13 行调用 levelOne() 27 但此时尚未定义 levelOne()。