-1

how would i add random operators to this? also how would i do another question lIke this?

import random
def main():
print ("This program prints 2 random number from 1,10")

num1 = random.randint(1,10)
num2 = random.randint(1,10)

total = (num1 + num2)
print (num1, "\n","+","\n",num2)

ans = int(input("enter answer"))

if total == ans:
print ("correct")

else:
print ("wrong")
def sum(num1,num2):
return num1 + num2
user3916096
  • 11
  • 1
  • 3
  • 1
    What exactly is your question? This is neither a code-writing nor a tutorial service. Based on the indentation, I'd say you should read https://docs.python.org/3/tutorial/index.html – jonrsharpe Nov 24 '14 at 19:00
  • 1
    You need to make sure you copy and paste the code properly. Python cares about spaces and you're missing an `=` in `num2 = random.randint(1,10)`. – Kyle_S-C Nov 24 '14 at 19:02
  • 1
    Also, I wouldn't redefine `sum`, it already exists and you may as well just write `num1 + num2`. – Kyle_S-C Nov 24 '14 at 19:03
  • thx guys i had to write the code and not paste itlol i fixed it – user3916096 Nov 24 '14 at 19:15

1 Answers1

0

I suggest you edit your question, because it is pretty hard to understand what are you asking about and please edit your indentation. Ok now to the code.

As I understand, you want to choose a random math operator (+, -, /, *). You can do this:

import operator
import random

# A list of possible operators (functions)
OP_LIST = [
    operator.add,
    operator.sub,
    operator.div,
    operator.mul
]

# Choose a random one!
op = random.choice(OP_LIST)

# And use it
print op(10, 20)

I suggest you take a look at the operator module, it provides a lot of useful functions for stuff like this.

Victor
  • 158
  • 8