Home > Enterprise >  How can I use a randomly chosen function as a condition in an if statement?
How can I use a randomly chosen function as a condition in an if statement?

Time:11-05

I want to start out by saying that I am at a very beginner level with Python and probably do not code in the most efficient way with it quite yet.

I'm trying to build a program that gives the user a simple math quiz. It randomly chooses a problem (addition, subtraction, division, or multiplication) and asks for user input to answer the question.

If the user gets it right, I want to congratulate them. If they get it wrong, I want to give them the correct answer. I made functions for each of different types of problems that I want to give the user and added them to a list. I then randomly chose one of the functions to be called and given to the user.

From here, I thought I would just assign a variable (prob) to the randomly chosen function and use it as a condition in a series of if else statements. This doesn't work. Instead, it skips over the message I want it to send back to them and just goes to my next input command. I'm guessing it's because I can't really assign a variable to the random choice function.

How else would I write the code to make it do what I want it to do?

import random

def problem():
  global add
  def add():
    num1 = random.randint(0, 999)
    num2 = random.randint(0, 999)
    global addition
    addition = num1   num2
    op1 = f' {num1}\n {num2}'
    print(op1)
  global sub
  def sub():
    num1 = random.randint(0, 999)
    num2 = random.randint(0, 999)
    global subtract
    subtract = num1 - num2
    op2 = f' {num1}\n-{num2}'
    if subtract < 0:
      sub()
    elif subtract > 0:
      print(op2)
  global mult
  def mult():
    num1 = random.randint(1, 12)
    num2 = random.randint(1, 12)
    global multiply
    multiply = num1 * num2
    op3 = f'{num1} * {num2}'
    print(op3)
  global div
  def div():
    num1 = random.randint(0, 150)
    num2 = random.randint(0, 150)
    global divide
    divide = num1 / num2
    op4 = f'{num1} / {num2}'
    if num1 % num2 == 0:
      print(op4)
    elif num1 % num2 != 0 or num2 == 0:
      div()
  global numlist
  numlist = [add, sub, mult, div]
  prob = random.choice(numlist)()
  answer = input()
  if prob == numlist[0] and answer == f'{add.addition}':
    print(f'Congrats! You got it right.')
  elif prob == numlist[0] and answer != f'{add.addition}':
    print(f'Incorrect. The answer is {add.addition}.')
  elif prob == numlist[1] and answer == f'{sub.subtract}':
    print(f'Congrats! You got it right.')
  elif prob == numlist[1] and answer != f'{sub.subtract}':
    print(f'Incorrect. The answer is {sub.subtract}.')
  elif prob == numlist[2] and answer == f'{mult.multiply}':
    print(f'Congrats! You got it right.')
  elif prob == numlist[2] and answer != f'{mult.multiply}':
    print(f'Incorrect. The answer is {mult.multiply}.')
  elif prob == numlist[3] and answer == f'{div.divide}':
    print(f'Congrats! You got it right.')
  elif prob == numlist[3] and answer != f'{div.divide}':
    print(f'Incorrect. The answer is {div.divide}.')
  done = input('Type "done" if you want to be done. Press Enter if you want another problem.\n')
  while done == '':
    problem()
    if done == 'done' or done == 'Done':
      break

problem()

CodePudding user response:

You can do it by replacing prob with a variable containing a random number generated with randrange function. Also, you had several errors in your code like add.addition that makes no sense because a function cannot have any attributes. I changed it to simply addition to fix all the compiler errors, so the final code is something like that:

import random

def problem():
  global add
  def add():
    num1 = random.randint(0, 999)
    num2 = random.randint(0, 999)
    global addition
    addition = num1   num2
    op1 = f' {num1}\n {num2}'
    print(op1)
  global sub
  def sub():
    num1 = random.randint(0, 999)
    num2 = random.randint(0, 999)
    global subtract
    subtract = num1 - num2
    op2 = f' {num1}\n-{num2}'
    if subtract < 0:
      sub()
    elif subtract > 0:
      print(op2)
  global mult
  def mult():
    num1 = random.randint(1, 12)
    num2 = random.randint(1, 12)
    global multiply
    multiply = num1 * num2
    op3 = f'{num1} * {num2}'
    print(op3)
  global div
  def div():
    num1 = random.randint(0, 150)
    num2 = random.randint(0, 150)
    global divide
    divide = num1 / num2
    op4 = f'{num1} / {num2}'
    if num1 % num2 == 0:
      print(op4)
    elif num1 % num2 != 0 or num2 == 0:
      div()
  global numlist
  numlist = [add, sub, mult, div]
  num = random.randrange(4)
  prob = numlist[num]()
  answer = input()
  if num == 0 and answer == f'{addition}':
    print(f'Congrats! You got it right.')
  elif num == 0 and answer != f'{addition}':
    print(f'Incorrect. The answer is {addition}.')
  elif num == 1 and answer == f'{subtract}':
    print(f'Congrats! You got it right.')
  elif num == 1 and answer != f'{subtract}':
    print(f'Incorrect. The answer is {subtract}.')
  elif num == 2 and answer == f'{multiply}':
    print(f'Congrats! You got it right.')
  elif num == 2 and answer != f'{multiply}':
    print(f'Incorrect. The answer is {multiply}.')
  elif num == 3 and answer == f'{divide}':
    print(f'Congrats! You got it right.')
  elif num == 3 and answer != f'{divide}':
    print(f'Incorrect. The answer is {divide}.')
  done = input('Type "done" if you want to be done. Press Enter if you want another problem.\n')
  while done == '':
    problem()
    if done == 'done' or done == 'Done':
      break

problem()
  • Related