I am an amateur python coder and i was wondering if it is possible to put a timer into my times table game and if possible where i could put it. Here's my code for my game help would be appreciated , thanks :)

``````import random
import time
print ("Hello , this is the mental maths game")
time.sleep(0.25)
print ("You will have to answer 50 questions on each different level and get 45 or more correct to proceed")
name = input("What is your name?")
print ("Okey, ",name," lets begin.")
difficulty = "bronze"
points = 0
def get_int(prompt=""):
while True:
try:
return int(input(prompt))
except ValueError:
print("Try again, value entered was not an integer.")

if difficulty == "bronze":
questions = 50

while questions > 0:
x = random.randint(1,8)
y = random.randint(1,8)
print ("What is ",x ," x ",y)
guess = get_int()
print ("Correct.")
time.sleep(0.25)
questions = questions - 1
points = points + 5
else:
print ("incorrect")
time.sleep(0.25)
questions = questions - 1
time.sleep(0.25)
print ("You got ",correct_answer," questions correct. ")
time.sleep(0.25)
print ("You may proceed to the next round")
difficulty = "silver"
points = points + 250
else:
print ("Unlucky you didn't get to the next round you were ",45 - correct_answer," answers short of passing.")
time.sleep(0.25)
print ("You scored ",points," points!")
time.sleep(0.25)
print ("Thanks for playing!")

if difficulty == "silver":
questions = 50
while questions > 0:
x = random.randint(6,15)
y = random.randint(5,13)
print ("What is ",x ," x ",y)
guess = get_int()
print ("Correct.")
time.sleep(0.25)
questions = questions - 1
points = points + 10
else:
print ("incorrect")
time.sleep(0.25)
questions = questions - 1
time.sleep(0.25)
print ("You got ",correct_answer," questions correct. ")
time.sleep(0.25)
print ("You may proceed to the final round. Good luck!")
difficulty = "gold"
points = points + 750
else:
print ("Unlucky. You failed to get to the last round you were",45 - correct_answer," answers short of passing.")
time.sleep(0.25)
print ("You scored ",points," points!")
time.sleep(0.25)
print ("Thanks for playing")
if difficulty == "gold":
questions = 50
print("WARNING! THIS LEVEL IS EXTREMELY HARD YOU MUST ANSWER ALL 50 QUESTIONS CORRECT!")
while questions > 0:
x = random.randint(11,20)
y = random.randint(11,18)
print ("What is ",x ," x ",y)
guess = get_int()
print ("Correct.")
time.sleep(0.25)
questions = questions - 1
points = points + 25
else:
print ("incorrect")
time.sleep(0.25)
questions = questions - 1
time.sleep(0.25)
print ("You got ",correct_answer," questions correct. ")
time.sleep(0.25)
points = points + 2500
print("Congratulations, you completed the game with ",points," points!")
if points == 6000:
print("You've unlocked the bonus round because you scored FULL POINTS! Lets see how much of a maths master you really are!")
difficulty = "platinum"
else:
print ("You didn't manage to complete the last level of the game you were",50 - correct_answer," answers short of passing.")
time.sleep(0.25)
print ("You scored ",points," points!")
if difficulty == "platinum":
questions = 50
print("WARNING! THIS LEVEL IS HARRDER THAN EXTREMELY HARD AND YOU NEED ALL 50 QUESTIONS RIGHT AGAIN BUT YOU GET ALOT OF POINTS REMIND ME AGAIN WHY AM I USINGS CAPS? OH WELL GOOD LUCK!")
while questions > 0:
x = random.randint(15,25)
y = random.randint(15,25)
print ("What is ",x ," x ",y)
guess = get_int()
print ("Correct.")
time.sleep(0.25)
questions = questions - 1
points = points + 50
else:
print ("incorrect")
time.sleep(0.25)
questions = questions - 1
time.sleep(0.25)
print ("You got ",correct_answer," questions correct. ")
time.sleep(0.25)
points = points + 5000
print("Congratulations, you completed the game (for real this time) with ",points," points!")
else:
print ("You failed to complete the game (For real) but you got ",points," points!")
time.sleep(0.25)
print ("You failed to complete the game (For real) with ",50 - correct_answer," answers short of completing the game (For real).")
``````

## All 2 Replies

Well. I don't know where to begin.

There is too much code. Seriously.
Please consider not to repeat almost the same code for every difficulty. You can factor it out, and make the whole thing better.
Like this:

``````import random
import time
from collections import namedtuple

def get_int(prompt=""):
while True:
try:
return int(input(prompt))
except ValueError:
print("Try again, value entered was not an integer.")

def print(*args,delay=0.25):
__builtins__.print(args)
time.sleep(delay)

level_info=namedtuple("level_info","order range_x range_y begin_message allow_correct end_point_increase point_increase")
levels={"bronze": level_info(
order=0,
range_x=(1,8),
range_y=(1,8),
begin_message="",
allow_correct=3,
end_point_increase=250,
point_increase=5),
"silver": level_info(order=1,
range_x=(6,15),
range_y=(5,13),
begin_message="",
allow_correct=3,
end_point_increase=750,
point_increase=10),
"gold":  level_info(order=2,
range_x=(11,20),
range_y=(11,18),
begin_message="VERY VERY HARD LEVEL",
allow_correct=3,
end_point_increase=2500,
point_increase=25)
}

print ("Hello , this is the mental maths game")
print ("You will have to answer 50 questions on each different level and get 45 or more correct to proceed")
name = input("What is your name? ")
print ("Okey, ",name," lets begin.")

points=0
for difficulty in sorted(levels, key=lambda x:levels[x].order):
info=levels[difficulty]
print (difficulty+ " level: ")
if info.begin_message: print (info.begin_message)
questions=5
while questions>0:
x=random.randint(*info.range_x)
y=random.randint(*info.range_y)
print ("What is ",x ," x ",y,delay=0)
guess = get_int()
print ("Correct.")
points+=info.point_increase
else:
print("incorrect")
questions-=1
print ("You got ",correct_answer," questions correct. " )
print ("You may proceed to the next round")
points += info.end_point_increase
else:
print ("Unlucky you didn't get to the next round you were ",info.allow_correct - correct_answer," answers short of passing.")
break
print ("You scored ",points," points!")
print ("Thanks for playing!")
``````

Do you mean you want to limit the amout of time the user has to answer the question or something else? A while loop or a 'return' from a function will work to limit the time

``````from datetime import datetime, timedelta
import time

current = datetime.now()
stop = current + timedelta(minutes=1)
ctr = 0
while current < stop:
current = datetime.now()
print current, "-->", stop
time.sleep(2)        ## avoid too much printing
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.