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


Messages In This Thread
Generate Random operator, take user input and validate the user - by mapypy - Feb-02-2021, 02:56 PM

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 884 Apr-24-2024, 05:47 AM
Last Post: Bronjer
  run SQL without user intervention python dawid294 0 237 Jan-19-2024, 01:11 PM
Last Post: dawid294
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,072 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Help on the User Interface Afia 1 546 Jul-21-2023, 07:22 PM
Last Post: snippsat
  restrict user input to numerical values MCL169 2 919 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,075 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,100 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,106 Dec-11-2022, 07:39 PM
Last Post: snippsat
  validate large json file with millions of records in batches herobpv 3 1,280 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Create SQL connection function and validate mg24 1 955 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