Python Forum
Generate Random operator, take user input and validate the user - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Generate Random operator, take user input and validate the user (/thread-32305.html)



Generate Random operator, take user input and validate the user - mapypy - Feb-02-2021

Hi,

I have the following code:

import random
number_one = random.randint(0, 100)
number_two = random.randint(0, 100)
 
rand_ops = ['+', '-', '/', '*']
 
while(True):
    correct_answer = eval (str(number_one) + random.choice(rand_ops) + str(number_two))
    trial = input('What is the correct answer for:'+ str(number_one) +random.choice(rand_ops)+ str(number_two)+'=' )
   
    if  int(trial) != int(correct_answer):
        print('That is incorrect. Try again. :(')
        continue
    else:
        print('That is correct. Great job!'':)')
        break
This code does not recognize the correct answer.
I would like to keep the structure of the code and keep it at a minimum to accomplish the following:

  • Generate two random numbers between 0 and 100
    • Generate a random operation ( +, -, /, * , ** )
      Print the statement [based on random values]:

e.g. random numbers are 10, 5
e.g. random operation is '-'
it should print 10 - 5 = ?
  • Get the input from the user
    • Validate the answer




RE: Generate Random operator, take user input and validate the user - buran - Feb-02-2021

you choose operator twice. there is no guarantee the question and the "correct" answer are the same operation.

As a side note - look at operator module, instead of using eval


RE: Generate Random operator, take user input and validate the user - mapypy - Feb-02-2021

(Feb-02-2021, 03:00 PM)buran Wrote: you choose operator twice. there is no guarantee the question and the "correct" answer are the same operation.

As a side note - look at operator module, instead of using eval

What is your recommendation to standardize the operation in this case?


RE: Generate Random operator, take user input and validate the user - buran - Feb-02-2021

(Feb-02-2021, 03:35 PM)mapypy Wrote: What is your recommendation to standardize the operation in this case?
select operator only once and assign it to variable. use that variable to ask the user and calculate the answer.

Note, in this case using eval will not create risk, but if you have input from untrusted source - it could be a problem


RE: Generate Random operator, take user input and validate the user - nilamo - Feb-03-2021

Instead of using eval(), using the operator module would look a little like this:
>>> import operator as op
>>> import random
>>> rand_ops = {"+": op.add, "-": op.sub, "/": op.truediv, "*": op.mul}
>>> num_one = random.randint(0, 100)
>>> num_two = random.randint(0, 100)
>>> op_key = random.choice(list(rand_ops.keys()))
>>> op_key
'+'
>>> print(f"what's {num_one} {op_key} {num_two} = ?")
what's 37 + 1 = ?
>>> answer = rand_ops[op_key](num_one, num_two)
>>> answer
38