Reputation: 11
I am trying to create a game where the user has to recall the numbers shown in the pop-up window. of they get any wrong, 'incorrect' is incremented by the number of numbers the have gotten wrong. if they get each number right, the score is incremented by 100. In this case, 300 should be outputted as the score.
The scores should only be incremented by 100 if what the user inputted is the same as 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 ()
Example of what is displayed when playing the game:
please enter the numbers you saw IN ORDER(press Enter when finished): 58
please enter the numbers you saw IN ORDER(press Enter when finished): 84
please enter the numbers you saw IN ORDER(press Enter when finished): 44
you have 100 points
Upvotes: 1
Views: 140
Reputation: 78
You haven't initialized the variable 'incorrect'. It will throw a NameError.
There is no need for the while loop. Replace 'while' with 'if' statement.
score = score + 1. No need for the correct variable there. (unless you want to tell the player how many numbers they got right. In that case, initialize correct = 0. Then increase count of correct every time player's answer is correct: correct += 1.
Instead of using a second 'if' statement to check if user has entered a correct number, just use 'else' statement.
Here's the fixed code:
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 ()
Upvotes: 1