Exerecise 14 error in "Learn Python the hard way" - python

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)

Related

Prompting and Passing

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

Not all arguments converted during string formatting operation

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

PyScripter Import ARGV

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

Utilizing %r within raw_input in python

Is it feasible to utilize %r within raw_input in python?
For context, I'm working on Zed Shaw's Exercise 12. (Great resource! The lessons are very helpful, and well paced.)
I'm playing around in the extra credit, trying to get raw_input to repeat what I typed in. I know I can accomplish the same thing with a print statement, but am curious if I can accomplish it within raw_input.
The code I'm typing is:
from sys import argv
script, firstname, lastname = argv
age = raw_input("Hello %r, what is your age? ") % firstname
print ("Ahh, Mr. %r, you are %r years old.") % (lastname, age)
The error I get is:
Traceback (most recent call last):
File "ex13a.py", line 5, in <module>
age = raw_input("Hello %r, what is your age? ") % firstname
TypeError: not all arguments converted during string formatting
Thanks in advance!
Your line should read
raw_input("Hello %r, what is your age? " % firstname)
instead of
raw_input("Hello %r, what is your age? ") % firstname
Otherwise, you would not format your "Hello %r, ..." string, but the resulting string of the call to raw_input.

getting error with learn python the hard way exercise 14

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.

Categories

Resources