##### Generate Random operator, take user input and validate the user
 Generate Random operator, take user input and validate the user mapypy Programmer named Tim Posts: 9 Threads: 6 Joined: Feb 2021 Reputation: Feb-02-2021, 02:56 PM 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 100Generate 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 userValidate the answer Reply Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Feb-02-2021, 03:00 PM 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 mapypy Programmer named Tim Posts: 9 Threads: 6 Joined: Feb 2021 Reputation: Feb-02-2021, 03:35 PM (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 Posts: 7,614 Threads: 133 Joined: Sep 2016 Reputation: Feb-02-2021, 03:41 PM (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 nilamo Last Thursdayist Posts: 3,445 Threads: 99 Joined: Sep 2016 Reputation: Feb-03-2021, 08:41 PM 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 File sorting by user-chosen category Bachelar 0 211 Aug-28-2021, 08:14 AM Last Post: Bachelar Generate random hex number ZYSIA 1 598 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 424 Jun-29-2021, 08:21 PM Last Post: RandomNameGenerator Python Help, New User, Computer Automation hexagonalyeti 2 340 Jun-25-2021, 11:43 AM Last Post: hexagonalyeti Unable to access the user input value given to daterangepicker klllmmm 1 631 May-26-2021, 09:16 PM Last Post: nilamo Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 603 May-18-2021, 06:42 PM Last Post: Skaperen Problem restricting user input in my rock paper scissors game ashergreen 6 1,104 Mar-25-2021, 03:54 AM Last Post: deanhystad Looking for a way to loop until user enters from a list? PythonW19 7 834 Mar-21-2021, 08:56 PM Last Post: PythonW19 User Subclasses holyghost 6 863 Mar-17-2021, 12:33 PM Last Post: buran User serial/pyserial to send messages to an arudino via terminal manually bkapadia 2 743 Mar-10-2021, 11:26 AM Last Post: Larz60+

Forum Jump:

### User Panel Messages

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