1

I am trying to write a simple math questions game in python3. My problem is timer function dones't return score. What am I missing here?

import random, time
n = 2
a = 0
def timer():
    stime = time.time()
    answ = soru()
    score = round(time.time()- stime,2)
    print(score,"ara zaman")
    if answ == True:
        return score
    else:
        return 0
def soru():
    return top()
def top():
    n1 = random.randint(1,n*5)
    n2 = random.randint(1,n*5)
    tansw = n1 + n2
    ques = print(str(n1)+"+"+str(n2)+"=?")
    pansw = input("-->")
    if pansw == tansw:
        print("doru")
        return True
    else:
        return False
for i in range(4):
    a += timer()
    print(a)
print(a)    
fferri
  • 18,285
  • 5
  • 46
  • 95

1 Answers1

1

As people mention above you need to compare two strings. Also, I thought it would be nice to generate both random numbers at once, Using numpy RNG.

import numpy as np
import time
n = 2
a = 0


def timer():
    stime = time.time()
    answ = soru()
    score = round(time.time()- stime,2)
    print(score,"ara zaman")

    if answ == True:
        return score
    else:
        return 0


def soru():
    return top()


def top():
    n1,n2 =  np.random.randint(1,2*n, size=2)
    tansw = n1 + n2
    ques = print(str(n1)+"+"+str(n2)+"=?")
    pansw = input("-->")

    if pansw == str(tansw):
        print("doru")
        return True
    else:
        return False

for i in range(4):
    a += timer()
    print(a)
print(a) 
Ashlou
  • 684
  • 1
  • 6
  • 21