1

I saw your bit explaining how to import these things and generate a random number using them but can you solve this problem. This is (the starting stages of) my program:

import random
from operator import add, sub, mul
for x in range(10):
    ops = (add, sub, mul)
    op = random.choice(ops)
    num1, num2 = random.randint(1,10), random.randint(1,10)
    int(input("What is %s %s %s?\n" % (num1, op, num2)))
    ans = op(num1, num2)

However when I execute this code this is printed: What is 8 1? and I was wondering how I would efficiently print this in a user friendly way such as: "What is 8 add 1?"

Thankyou if you solve this!

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 2
    @Cyber though it should be noted that the relevant answer in that duplicate is [yours](http://stackoverflow.com/a/26261125/1599111), not the accepted one. The OP has already seen the light and is using the `operator` module instead of `eval`, so mapping the functions to the symbols in a dict is the next logical step ;-) – Lukas Graf Oct 16 '14 at 20:37
  • Do you want it as "**What is 8 add 1?**" or "What is 8 plus 1?" because it's working fine. [ideone.link](http://ideone.com/o1jIEN) – ABcDexter Oct 16 '14 at 20:46

3 Answers3

2

Maybe use a dictionary instead of a tuple.

import random
from operator import add, sub, mul
for x in range(10):
    ops = {'+': add, '-': sub, '*': mul}
    op = random.choice(ops.keys())
    num1, num2 = random.randint(1,10), random.randint(1,10)
    int(input("What is %s %s %s?\n" % (num1, op, num2)))
    ans = ops[op](num1, num2)
Cuadue
  • 3,769
  • 3
  • 24
  • 38
  • after pasting this solution into python you get this error: Traceback (most recent call last): File "/Users//Documents/Random.py", line 5, in op = random.choice(ops.keys()) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/random.py", line 256, in choice return seq[i] TypeError: 'dict_keys' object does not support indexing Whats wrong with this?? – DaSweatyGooch Nov 04 '14 at 19:40
  • In python3, change `opts.keys()` to `list(opts.keys())` because in py3 `dict.keys` is an iterator, and `random.choice` needs to know the length, and does not automatically evaluate iterators (as it should not). – Cuadue Nov 05 '14 at 17:58
1

As Luke said, op.name would print the operator used. If you want explicit +/-/* or added to, multiplied by/subtracted from you can refer IDEONE

if op == mul:
        int(input("What is %s %s %s?\n" % (num1, 'multiplied by', num2)))
elif op == add:
        int(input("What is %s %s %s?\n" % (num1, 'added to', num2)))
elif op == sub:
        int(input("What is %s %s %s?\n" % (num1, 'subtracted from', num2)))
else:
        print ("randomise error, sorry.")
ABcDexter
  • 2,751
  • 4
  • 28
  • 40
0

Simply changing

(num1, op, num2)

to

(num1, op.__name__, num2)

does what you're asking for. Of course, you'll probably want to print +, - or *, and that will require a simple if/elif structure. I'll let you figure that out ;D

Luke Yeager
  • 1,400
  • 1
  • 17
  • 30