mardi 2 novembre 2021

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

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()

Aucun commentaire:

Enregistrer un commentaire