Skip to content Skip to sidebar Skip to footer

Is A Mathematical Operator Classed As An Interger In Python

in python is a mathematical operator classed as an interger. for example why isnt this code working import random score = 0 randomnumberforq = (random.randint(1,10)) randomoperato

Solution 1:

You can't just concatenate an operator to a couple of numbers and expect it to be evaluated. You could use eval to evaluate the final string.

answer = eval(str(randomnumberforq)
              + operator[randomoperator] 
              + str(randomnumberforq))

A better way to accomplish what you're attempting is to use the functions found in the operator module. By assigning the functions to a list, you can choose which one to call randomly:

import random
from operator import mul, add, subif __name__ == '__main__':
    score = 0
    randomnumberforq = random.randint(1,10)
    randomoperator = random.randint(0,2)
    operator = [[mul, ' * '],
                [add, ' + '], 
                [sub, ' - ']]
    answer = operator[randomoperator][0](randomnumberforq, randomnumberforq)
    useranswer = input(str(randomnumberforq) 
                       + operator[randomoperator][1] 
                       + str(randomnumberforq) + ' = ')
    if answer == useranswer:
        print('correct')
    else:
        print('wrong')

Solution 2:

That depends on what you're trying to do. You've given us no sample input or output, no comments, and no error message.

It looks like you're trying to write a simple practice engine for arithmetic. If so, then your basic problem is that you don't understand the operations allowed in programming. You can't just throw symbols in a row and expect the computer to figure out how it's supposed to combine them. Your assignment statements for answer and useranswer are structurally flawed. The first gives you a list of strings; the second dies because you tried to convert a symbol (such as *) to an integer.

For more advanced user, I would recommend the "evaluate" operation. For you, however ... When you pick the random operator, you'll need to check to see which one you got. Write a 3-branched "if" to handle the three possibilities. Here's what the head of the first might look like:

if randomoperator == 0:
    operator = '*'
    answer = randomnumberforq * randomnumberforq
elif: ...

Note that the two numbers in the operation are the same. If you want different numbers, you have to call randint twice.

Does this get you moving ... at a coding level with which you're comfortable?

Solution 3:

You try to convert a string to an integer, but which isn't a number:

int(operator[randomoperator])

Your operatators in the array "operator" are strings, which don't represent numbers and can't be converted to integer values. On the other hand the input() function desires string as parameter value. So write:

...= input(str(numberValue) + operatorString + str(nubmerValue))

The + operator can be used to concat strings. But Python requires that the operands on both sides are strings. Thats why i added the str() functions to cast the number values to strings.

Post a Comment for "Is A Mathematical Operator Classed As An Interger In Python"