gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:19:58 25 4
gpt4 key购买 nike

我正在尝试创建一个游戏,用户必须记忆弹出窗口中显示的数字。如果他们犯错了,“不正确”就会随着犯错的数字数量而增加。如果他们答对了每个数字,分数就会增加 100。在这种情况下,应该输出 300 作为分数。如果用户输入的内容与 array[i]

相同,则分数只能增加 100
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 分

最佳答案

  1. 您尚未初始化变量“不正确”。它将抛出一个 NameError。

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

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

  4. 无需使用第二个“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 ()

关于python - 如何每次检查用户输入的内容与数组[i]中的内容相同时将分数增加100?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57750231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com