Python 当检查用户输入的内容是否与数组[i]中的内容相同时,如何每次将分数增加100?

Python 当检查用户输入的内容是否与数组[i]中的内容相同时,如何每次将分数增加100?,python,if-statement,while-loop,Python,If Statement,While Loop,我正在尝试创建一个游戏,用户必须回忆弹出窗口中显示的数字。在他们出错的数字中,“不正确”是由出错数字的数量增加的。如果每个数字都正确,分数将增加100。在这种情况下,应输出300作为分数。 如果用户输入的内容与array[i] import random array = [] #this appends three random numbers into an empty array for i in range(3): randomNumber = random.randint(0

我正在尝试创建一个游戏,用户必须回忆弹出窗口中显示的数字。在他们出错的数字中,“不正确”是由出错数字的数量增加的。如果每个数字都正确,分数将增加100。在这种情况下,应输出300作为分数。 如果用户输入的内容与
array[i]

import random

array = []

#this appends three random numbers into an empty 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(1250, lambda: root.destroy())
    root.mainloop()   
randomNumberDisplay()


#this function requires the user to enter the numbers displayed.
score = 0
def levelOne ():
    incorrect = 0
    i = 0
    x = len(array)

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
    while userNumber != array[i]:
        print ("the numbers where: ", array[i])
        incorrect = incorrect +1
        ("you got ", incorrect, "wrong")

    if userNumber == array[i]:
        score = correct + 100
        i = i + 1
    print ("you have ",score, "points")
levelOne ()
玩游戏时显示的内容示例:

请按顺序输入您看到的数字(完成后按enter键):58

请按顺序输入您看到的数字(完成后按enter键):84

请按顺序输入您看到的数字(完成后按enter键):44

你得了100分

  • 您尚未初始化变量“不正确”。它将抛出一个名称错误

  • 不需要while循环。将“while”替换为“if”语句

  • 分数=分数+1。这里不需要正确的变量。(除非你想告诉玩家他们答对了多少个数字。在这种情况下,初始化correct=0。然后每当玩家的答案正确时,增加correct的计数:correct+=1

  • 与其使用第二个“if”语句检查用户输入的数字是否正确,不如使用“else”语句

  • 以下是固定代码:

        import random
        array = []
    
        #this appends three random numbers into an empty 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(1250, lambda: root.destroy())
            root.mainloop()   
        randomNumberDisplay()
    
    
        #this function requires the user to enter the numbers displayed.
        score = 0
        def levelOne ():
            incorrect = 0
            x = len(array)
            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[i]:
                print ("the numbers where: ", array[i])
                incorrect = incorrect +1
                print("you got ", incorrect, "wrong")
    
            else:
                score = score + 100
                i = i + 1
                print ("you have ",score, "points")
        levelOne ()
    

    谢谢,我现在看到了我的错误,但我希望程序检查输入的每个数字是否与数组[i]中的数字相同。对于每个错误的数字,“不正确”应该增加1。对于每个正确的数字,分数应该增加100。我只是在努力使评分系统正确。