I'm doing exercise 14 from the Zed Shaw's "Learn python the hard way" but having this problem. I have even tried copy pasting the code from the book but it always gives me that error. Here's my code:
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 some questions"
print ("Do you like me?") % (user_name)
likes = raw_input(prompt)
print ("Where do you live %s") % (user_name)
lives = raw_input(prompt)
print ("What computer do you use %s") % user_name
uses = raw_input(prompt)
print """
Alright so I asked you if you liked me and you said %r. You live in %r and you use
%r computer. Thanks
""" % (likes, lives, uses)
What's causing this error?
This line looks wrong:
print ("Do you like me?") % (user_name)
Change the statement
print("Do you like me? %s") % (user_name)
You gave the variable as an argument but you didn't have a placeholder for the argument. That's why it cannot convert the argument to string during formatting.
For what I understand the problem is with this line
print("Do you like me? %s") % (user_name)
and also with the fact that you don't pass an argument to the script, which is supposed to be bound to the user_name variable (this is why you get undefined).
Call the script like this:
$ python filename.py Muhammad
Related
I am running my program on Jupyter-notebook
%%file ex14.py
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 = (input(prompt))
print ("Where do you live %s?" % user_name)
lives = input(prompt)
print ("What kind of computer do you have?")
computer = input(prompt)
In the next cell
%%!
python ex14.py argument
After this program is not asking for the user inputs. Any guidance?
The same program is executing fine in terminal
Full error displayed in Jupyter
`["Hi argument, I'm the ex14.py script.",
"I'd like to ask you a few questions.",
'Do you like me argument?',
'> Traceback (most recent call last):',
' File "ex15.py", line 7, in <module>',
' likes = (input(prompt))',
'EOFError: EOF when reading a line']`
Because in Jupyter Notebook
%%!
python ex14.py argument
return your value in List. Use this %run instead of %%!
python
%run ex14.py argument
I am getting an error when I write this code. How do I rewrite this that it works? I am following the book and it is telling as it is.
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)
Error
Traceback (most recent call last):
File "C:\python-files\ex14.py", line 5, in <module>
script, user_name = argv
ValueError: need more than 1 value to unpack
So I've ran my code several time, I have it word for word and line for line in the book yet theres an error that keeps popping up:
Traceback (most recent call last):
File "ex14.py", line 25, in <module>
''' (likes, computer, house)
TypeError: 'str' object is not callable
I had to change the input once to "PS C:\Users\J> python ex14.py YourNameHere" for the exercise, but this is when the error occurs. I do not understand why the string is not callable. My full code is below:
from sys import argv
script, user_name, = argv
prompt = '>'
print "Hi %s, I'm the %s script." % (script, user_name)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input (prompt)
print "What kind of computer do you have?"
computer = raw_input (prompt)
print "Where do you live %r?" %user_name
house = 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, computer, house)
Any help would be most appreciated.
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, computer, house)
In this line here (which is the issue) you're missing a the "%", which should go as such:
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, computer, house)
Not too sure how to get the following code to work properly in PyScripter.
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 question."
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 the fuck that is!
And you have a %r computer. It's obviously a piece of shit!
""" % (likes, lives, computer)
Thanks.
As you unpack the argv to 2 variable you need to pass user_name in terminal.
from sys import argv
script, user_name = argv
print user_name
Demo:
/Desktop$ python p.py userrrr
userrrr
but if you dont pass the username at runtime you will get that error :
~/Desktop$ python p.py
Traceback (most recent call last):
File "p.py", line 51, in <module>
script, user_name = argv
ValueError: need more than 1 value to unpack
I am following zed shaw's learn python the hard way and am following exercise 14. Here's the program I am talking about:
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)
Now, I run this program in powershell terminal using the command
python e:\python\ex14.py
and I get the following error message:
Traceback (most recent call last):
File "e:\python\ex14.py", line 3, in (module)
script, user_name=argv
ValueError: need more than 1 value to unpack.
Now, I am not sure what the problem is. The only reason could be that I am typing the file path instead of typing the filename only.
This script expects to take an argument at the command line. You're not providing one.
At the terminal, type python e:\python\ex14.py YourNameHere.