Samuel Harrison Samuel Harrison - 3 months ago 33
Python Question

Python 2.7 TypeError: Unsupported operand type(s) for %:'None type' and str

I've got an issue where when i try to print 'filename' i get this error

line 101, in purchase_code_fn
print("QR Code Created: %s") %(filename)
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'


Below is the offending function.

def purchase_code_fn():
count=+1
name = raw_input("Name: ")
email_prompt = raw_input("Please enter your email address: ")
userid = uuid.uuid4()
filename = (str(email_prompt)+str(count))
print("QR Code Created: %s") %(filename)
qr_code_fn(email_prompt, userid)


A pointer in the right direction would be fantastic.

Cheers!

Answer

I think you're trying to run Python 2 code with Python 3. In Python 3, print is a function, but a statement in Python 2.

The print function had already been executed and the formatting did not come a priori as you intended or would have in Python 2. So you are trying to format the None returned by print, which is clearly not going to work.

You should remove the closing parenthesis trailing the string:

print("QR Code Created: %s" % filename)