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
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 680 Yesterday, 07:07 AM
Last Post: Bronjer
  run SQL without user intervention python dawid294 0 230 Jan-19-2024, 01:11 PM
Last Post: dawid294
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,053 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Help on the User Interface Afia 1 538 Jul-21-2023, 07:22 PM
Last Post: snippsat
  restrict user input to numerical values MCL169 2 907 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,064 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,077 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,084 Dec-11-2022, 07:39 PM
Last Post: snippsat
  validate large json file with millions of records in batches herobpv 3 1,264 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Create SQL connection function and validate mg24 1 937 Sep-30-2022, 07:45 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