-5

is the code he wants me to enter that fails

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)

is the code I modified after seeing errors and knowing he uses python 2, I've just been making corrections to his code as I find them online.

from sys import argv

script, user_name = argv
prompt = '> '

print ("Hi" user_name: %s, "I/'m the", %s: script.)
print ("I;d like tok ask you a few questions")
print ("Do you like me %s") % (user_name)
likes = input(prompt)

All %s, %d %r have failed for me. Is this a python 2 convention? Should I be using something else?

for example

foo = bar 
print ("the variable foo %s is a fundamental programming issue.)

I have tried using tuples? as in:

print ("the variable foo", %s: foo, "is a fundamental programming issue.")

with no success

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    Note that you need to format the string **before** passing it to the `print` function: `print("Do you like me %s" % user_name)`. Otherwise you're trying `None % user_name`... – jonrsharpe May 12 '15 at 11:11
  • You use the `%` operator on the return value of `print`. It should look like this: `print("Do you like me %s" % user_name)`. – Matthias May 12 '15 at 11:12
  • Duplicate of e.g. http://stackoverflow.com/q/23372824/3001761, http://stackoverflow.com/q/22070888/3001761, http://stackoverflow.com/q/7081905/3001761, etc... **Google your error messages**. – jonrsharpe May 12 '15 at 11:16
  • possible duplicate of [Python string formatting: % vs. .format](http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format) – Jasper May 12 '15 at 11:19
  • Honestly, there should be a tag for Learning Python the Hard Way. The five most abhorred words to a Python programmer. – Malik Brahimi May 12 '15 at 11:39
  • I did Google the error – Deindichter May 12 '15 at 11:51
  • Sorry for dupes. I'll try to search more thoroughly before posting next time. – Deindichter May 12 '15 at 12:15

3 Answers3

0

You should try to use string.format()

For example:

some_string = 'My name is {name} and i live in {location}'
some_params = {
    'name': 'Tim',
    'location': 'Germany'
}

print(some_string.format(**some_params))

Which will result in

My name is Tim and i live in Germany

For a more specific documentation, look at the Format Specification Mini-Language

Nevermind, the problems in your second example are:

  • Remove the gap between print and the starting bracket (
  • Build the strings before the print statement. Sometimes it looks like there are missing &, * and %S's
fechnert
  • 1,215
  • 2
  • 12
  • 30
0

As said by tim we can use string.format()

For formatting the string you can use index also. i.e., Your code will be look like the below one,

from sys import argv

script = argv[0]
user_name = argv[1]


print "Hi {0} I am the Script {1}".format(user_name,script)
print ("I;d like tok ask you a few questions")
print "Do you like me {0}".format(user_name)
user3707514
  • 45
  • 1
  • 5
0

This should fix your issue as when using brackets with print all formatting should be inside of the brackets:

from sys import argv

script, user_name = argv

prompt = '> '

print ("Hi %s, Im the %s" % (user_name, script))

print ("I;d like tok ask you a few questions")

print("Do you like me %s" % (user_name))

likes = input(prompt)
agentred5
  • 61
  • 8
Ben Magill
  • 38
  • 3
  • 8