user2801425
user2801425

Reputation: 11

TypeError when printing the result from input()

I'm using python 3.3 and I'm trying to use the %s function but I get an error. I put print("you're a %s man") % (input("put an adjective"))

then I run it in the module and get this

you're a %s man put an adjectivefunny Traceback (most recent call last): File "C:\Users\Me\Desktop\coding\mie.py", line 1, in <module> print("you're a %s man") % (input("put an adjective")) TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

Upvotes: 1

Views: 53

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250951

Correct syntax:

>>> print("you're a %s man" % input("put an adjective: "))
put an adjective: foo
you're a foo man

print() returns None in py3.x, so you were trying to apply string formatting to that None.

>>> None % "foo"
Traceback (most recent call last):
  File "<ipython-input-2-1aa4f0c0cbbd>", line 1, in <module>
    None % "foo"
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

Upvotes: 5

Related Questions