I made this program this morning, the problem is that when I go to run it it says:

> Traceback (most recent call last):
>   File "C:/Python33/Test random number creator.py", line 4, in <module>
>     print  (x)('x')(y)
> TypeError: 'NoneType' object is not callable
>     `

Here is the program:

from random import randrange, uniform
x= randrange (0,100)
y= randrange (0,100)
print  (x)('x')(y)
Aa = int(input('Type your answer here:   '))
if Aa==x*y:
    print ('Correct!')
else:
    print ('incorrect')

How would you fix this?

I'm guessing you're using Python 3.x where print() takes exactly one argument. Instead of print (x)('x')(y) you need something like ("something like" because I don't have Python 3 to test on): print(x + 'x' + y)

Edited 3 Years Ago by rrashkin

Try this ...

from random import randrange

x = randrange (0,100)
y = randrange (0,100)
#print  (x)('x')(y)
print("{}x{}".format(x, y))

answer = int(input('Type your answer here:   '))
if answer == x*y:
    print ('Correct!')
else:
    print ('Incorrect')

I'm guessing you're using Python 3.x where print() takes exactly one argument.

That's not true. In Python 3 print takes arbitrarily many arguments. Further the OP's code won't work in Python 2 either.

The syntax to call print (or any other function) with multiple arguments in Python 3 is print(arg1, arg2, etc). In Python 2 it's print arg1, arg2, etc. So the difference is the lack of parentheses. In either case print (arg1)(arg2)(etc) is wrong.

In Python 2 it is interpreted as calling arg1 with arg2 as its argument and then calling the result of that with etc as its argument and then printing the result. Of course that only works if arg1 is a function (or other callable object) and arg1(arg2) also returns a function (or other ...).

In Python 3 it is interpreted as calling print with arg1 as the argument, then calling the result of that with arg2 as the argument and then calling the result of that with etc as the argument. Since the result of print(arg1) is None (no matter what arg1 is), this will lead to the error message that the OP is getting.

This question has already been answered. Start a new discussion instead.