How to pull an operator out of the list and do an operation!!

Asked 2 years ago, Updated 2 years ago, 19 views

import random op_box = ["+", "-",//",*"] #4 operations.

print("Hello, I'm an arithmetic problem generator.")

while True: x = random.randint(50, 100) y = random.randint(1, 20) z = random.randomint(0,3) #Number for random pull from the box

print(x,str(op_box[z]),y,"=",end='')
answer = int(input())
if answer == x+y or answer == x-y or answer == x//y or answer == x*y:
    print ("Correct")
else:
    print("You can do better next time, right?"")

Question..Pick the zth character from the op_box (list containing operators) and

Is there a way to process the operation by inserting the generated random numbers between x and y?

x,str(op_box[z]),y like this! I think there's a connection, but I don't know. If I write it like above, there will be a Syntax error!

python

2022-09-22 14:47

2 Answers

Is this enough?

a = '3'
b = '4'
sign = '+'
s = a + sign + b
eval(s)

7


2022-09-22 14:47

op_box = ["+"-"-"*"",//"] #4 operators in the box.
x = random.randint(50, 100)
y = random.randint(1, 20)
z = random.randomint(0,3) #Generate random numbers for random extraction from the box
Random_Question = generate a random expression based on str(x)+str(op_box[z])+str(y) #randomz
print(Random_Question,"=",end='')
answer = int(input())
if answer == x+y or answer == x-y or answer == x//y or answer == x*y:
    print ("Correct")
else:
    print("You can do better next time, right?"")


2022-09-22 14:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.