-2
from sys import argv

script, user_name = argv
prompt = '> '

print ("Hi %s, I'm the %s script.") % (user_name, script)
print ("I'd like to ask you a few questions.")
print ("Do you like me %s?") % user_name
likes = raw_input(prompt)

print ("Where do you live %s?") % (user_name)
lives = raw_input(prompt)

print ("What kind of computer do you have?")
computer = raw_input(prompt)

print """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)

While running this (Python 3) code in PowerShell I am getting

Type error 'unsupported operand type(s) for %:'Nonetype' and 'str''

What is the error here?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • For the future: Please also post the full stack trace of the error you are seeing. Otherwise we just have to guess *where* the error occurs. If you look at it yourself, you could have seen that the error appears on the line of the `print` call, so it has nothing to do with `argv` or anything. – poke Jul 19 '17 at 12:01
  • 1
    This code seems to be very broken. You are using python3? Yet dont use the print function (with parenthesis) and you do use `raw_input`. Are you sure its not py2? – Paul Rooney Jul 19 '17 at 12:01
  • @PaulRooney Once fixing the error from the question, OP will likely see a `NameError` for the `raw_input` use. – poke Jul 19 '17 at 12:03
  • 1
    You're **not** running **this** script in Python 3. It throws `SyntaxError`. Please recheck that code in your question matches the code you're actually running and produces the *same problem*. – Antti Haapala -- Слава Україні Jul 19 '17 at 12:05

1 Answers1

2
print ("Hi %s, I'm the %s script.") % (user_name, script)

print is a function in Python 3, so this is doing something different to what you expect. The first set of parentheses belong to the print function call, so what you have is this:

print("Hi %s, I'm the %s script.")  %  (user_name, script)
#    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    
# only that is the argument to the print call

So if you split this up in separate statements, it would look like this:

print_result = print("Hi %s, I'm the %s script.")
format_args = (user_name, script)

print_result % format_args

Now, calling print() returns nothing, i.e. None. So you are essentially doing the following:

None % format_args

And that causes the exact error you are seeing.

What you want to do instead is ensure that the string formatting happens for the argument passed to the print call, like so:

print("Hi %s, I'm the %s script." % (user_name, script))

Note that you do not need to put a string in additional parentheses. So there is only a single opening parenthesis for the print call which is closed at the very end.


As Paul Rooney pointed out in the comments, once you have fixed those print calls, you will likely run into a NameError for your use of raw_input. raw_input does not exist in Python 3, so you will need to fix that too. See also this question on that topic.

poke
  • 369,085
  • 72
  • 557
  • 602