Python Forum
Generate Random operator, take user input and validate the user
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Generate Random operator, take user input and validate the user
#1
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

Reply
#2
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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?
Reply
#4
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
mapypy likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Generate random hex number ZYSIA 1 211 Jul-16-2021, 09:21 AM
Last Post: DeaD_EyE
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 253 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  Python Help, New User, Computer Automation hexagonalyeti 2 229 Jun-25-2021, 11:43 AM
Last Post: hexagonalyeti
  Unable to access the user input value given to daterangepicker klllmmm 1 483 May-26-2021, 09:16 PM
Last Post: nilamo
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 439 May-18-2021, 06:42 PM
Last Post: Skaperen
  Problem restricting user input in my rock paper scissors game ashergreen 6 902 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Looking for a way to loop until user enters from a list? PythonW19 7 637 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  User Subclasses holyghost 6 715 Mar-17-2021, 12:33 PM
Last Post: buran
  User serial/pyserial to send messages to an arudino via terminal manually bkapadia 2 623 Mar-10-2021, 11:26 AM
Last Post: Larz60+
  LinkedList by User Input SantiagoPB 5 968 Mar-03-2021, 09:54 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020