lundi 14 juin 2021

A faster way to have two random numbers that always divisible?

today I wrote a simple math game that I can practice mental math. My concerns is to make sure two number are always divisible. I have tried the while loop to add 1 until it divisible but it took too long:

import random
import operator

def random_problem():
  operators = {
    '+': operator.add,
    '-': operator.sub,
    'x': operator.mul,
    ':': operator.truediv
  };

  num1 = random.randint(1000,9999)
  num2 = random.randint(num1,9999)

  operation = random.choice(list(operators.keys()))

  if operation == ':':
    while num1 % num2 != 0:
      num2 += 1
  
  answer = (operators.get(operation)(num1, num2))
  print(f'What is {num1} {operation} {num2}')
  return answer

So any ideas to make this process faster? Thanks for your answers.




Aucun commentaire:

Enregistrer un commentaire