-3

The code I have at the moment is:

#Imports the random module
import random

#Asks the user to input there name
print("What's your name ?")
name = input()
#Resets the score
score = 0

#Loops ten times for ten questions

for i in range(1, 11):

    #Generates two numbers and an operator
    x = random.randint(1, 20)
    y = random.randint(1, 20)
    opIndex = random.randint(1, 3)

    #Checks the opIndex, gives an operator and calculates the result
    if opIndex == 1:
        op = '+'
        ans = x + y
    elif opIndex == 2:
        op = '-'
        ans = x - y
    else:
        op = '*'
        ans = x * y

#asks the user if they want to take the test
start = input("Are you ready to take the test? (y/n):")
while start.lower() =='y':

    #Prints out the question for user input (Too much concatenation)
    inp = input('Question ' + str(i) + ': ' + str(x) + op + str(y) + '= ')

    #Checks if the input is the same as the answer to the sum
    if int(inp) == int(ans):

        #Tells the user that they were correct and adds 1 to their score
        print('Correct answer, well done!\n')
        score += 1
    elif start.lower() =='n':
        print ("take your time to prepare")
        start = input ("Are you ready now ? [y/n]")
    else:
        print('Incorrect answer, bad luck!\n')

#After the 'for' loop the final score is printed
print('\n' + name + ' your final score was ' + str(score) + ' out of 10!')

But I need it to loop back to the question are you ready to start if the user answers with a 'n' .

Thanks if you can help.

dreyescat
  • 13,558
  • 5
  • 50
  • 38
  • 1
    You should add your "question-generator" inside a function that you can call where you need it, as many times as you need – AlvaroAV Nov 26 '14 at 09:48

1 Answers1

0
while True:
start = raw_input("Are you ready to take the test? (y/n): ")
if start=="n":
    print("take your time to prepare")

start=raw_input("Are you ready now?[y/n]: ")
if start=="n":
    continue

HERE