0

Hello I'm new to python and programming in general (just started last night!) and I've written a tiny program thats pasted below:

name = str(input("please state your name: ")) 
print ("Welcome %s, your doomesday weapon of impolite mutant pigeons is ready for launch") %name

However this keeps returning this error:

Traceback (most recent call last):
File "prog.py", line 2, in <module>
print ("Welcome %s, your doomesday weapon of impolite mutant pigeons is ready for launch") %name
TypeError: unsupported operand type(s) for %: 'NoneType' and 'str'

Now I know that the usual answer to this kind of problem is to convert my input from a string to an integer and adjust the used operand type to match. However in this case, I actually want to use the input in string form so wheres the issue at?

P.S I'm using Ideone.com for compiling this code.

CrazyChucky
  • 3,263
  • 4
  • 11
  • 25

3 Answers3

1
... launch" %name)

instead of

... launch") %name
user1651640
  • 181
  • 4
0

The string-formatting % needs to go inside the (); the print function returns None, and you're trying to format the result.

Assuming you're using Python 3, your initial call to str() is also unnecessary; input() returns a string. (If you're using Python 2, you should use raw_input() instead of input(), which also returns a string.)

Wooble
  • 87,717
  • 12
  • 108
  • 131
0

There are two parts to this question:

name = "andy"
print "Welcome %s, your doomesday weapon of impolite mutant pigeons is ready for launch" %name

If this works (then you are using Python 2), if it doesn't it is becuase in Python 3 you need to enclose print statements in brackets, and will get a SyntaxError.

name = "andy"
print ("Welcome %s, your doomesday weapon of impolite mutant pigeons is ready for launch" %name)

The second is that Ideone.com does not accept user input, and hence the string name is left as None (which is not a string) which explains the TypeError.

name = None
print ("Welcome %s, your doomesday weapon of impolite mutant pigeons is ready for launch" % name)
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
  • Well the program works but now I've run into another issue. For some reason when I make the following statement: if name == "John", I get an error saying that my syntax is invalid. If you can offer a solution to this plus the negative version (name != "John"), it would be much appreciated. – RationalMystic Sep 10 '12 at 14:44
  • @RationalMystic Could this be because you are missing a `:` at the end of the line? (it's hard to see without the full code.) You should look into a Python tutorial e.g. Dive into Python. Also, you should upvote and accept helpful answers :). – Andy Hayden Sep 10 '12 at 15:34
  • lol I know what you're trying to say but I don't have enough reputation points to actually upvote people yet. I would if I could. – RationalMystic Sep 10 '12 at 20:28