# How do I initialize an array in python?

from random import random, randint
correct = 0
n = 1
j = 0
k = 1
a = randint(0,10)
b = randint(0,10)
test = True
questions = []
print("Welcome to the multiplication math quiz")
print("In this quiz there will be 5 questions")
print("Good Luck!")
while test == True:
for i in range(6):
questions.insert(j,a)
questions.insert(k,b)
x = questions[i+i]
y = questions[i+n]
n+=1
print(x, "*", y, "=")
print("Correct")
correct = correct + 1
else:
print("Incorrect")
n += 1
j += 2
k += 2
questions.clear()
print("You got", correct, "/" , "5" , " correct and have a percentage of " , (correct/5)*100 , "%")
correct = 0
print("would you like to play again? yes/no")
user = raw_input()
user.lower()
if user is "yes":
continue
else:
test = False

This is my code but when ever I try to fetch from my array it says it's out of scope.

–1 vote
answered Feb 15, 2020 by (2,990 points)
You're trying to fetch from a position that does not exist in your list. "i+i" at minimum gives you 0 in first loop iteration, but 2 in the second, 4 in the third, 6 in the fourth and so on. Similarly with "i+n" but that goes even faster since you have 2 lines with n+=1 which effectively gives you n+=2 per iteration.

Both could be swapped with "i" and "i+1" respectively or "0" and "1" if you want to ".clear()" your list. That's not the only problem though:

1. Unless you intend to put your numbers in, well, kind of random place in your array you should use ".append()" instead of ".insert()". Difference between those is in insert you decide after which element you add your new element, while append just adds it at the end of the list (array).

2. Your code doesn't check user answer correctly since "answer" variable has type of string (basically text) and the type of "(x*y)" is an integer (number). Because of this algorithm thinks they are always different no matter what.

3. I suppose you think "a" and "b" always a randomized number from 0 to 10 on each question, but no. They are picked once and then they stay the same. If they were in "for" loop they would be different on each loop iteration.

I don't remember whether that was all or not but anyways here's my version of your code with example fixes:

https://onlinegdb.com/SkvbrKN7U
+1 vote
answered Feb 18, 2020 by (970 points)

I took the liberty to reorganize your script. Please check it here and/or test it in https://onlinegdb.com/S1RTZotQL

print("Welcome to the multiplication math quiz!")
print("In this quiz there will be 5 equations.")
print("Good Luck!\n")

from random import randint
import sys, os

used = ["None*None"]
gi, gcorrect = 0, 0
equations = 5 # number of equations to ask per game

def clear():
os.system("cls" if os.name == "nt" else "clear")

while True:
correct = 0
for _ in range(equations):
gi += 1
tries = 0
x, y = None, None
while "{}*{}".format(x, y) in used:
if tries > 99:
print("I think I asked all the numbers. Thanks for playing!")
sys.exit(1)

x, y = randint(1,10), randint(0,10)
tries += 1

used.append("{}*{}".format(x, y))
answer = input("How much is {} * {}? ".format(x, y))
try:
except:
pass

sys.stdout.write("\033[F")
print("\rHow much is {} * {}? {}".format(x, y, answer), end="")

if answer == x * y:
correct += 1
gcorrect += 1
else:
print("\tSorry... The correct answer is {}.".format(x * y))

print("\nYou got {}/5: {:0.1f}% correctness.".format(correct, correct*100/5))
choice = input("Hit Enter to play again or send any letter to quit: ")

if choice == "":
clear()
else:
if gi > equations:
clear()
print("Your global score was {}/{}: {:0.1f}% correctness.".format(gcorrect, gi, gcorrect*100/gi))
break

#M# keyboard input, random integer number, system/os call, console and previous line clear, carriage return, list check/append, iteration, integer input check, error prevention, format number/percentage