PyScripter Import ARGV - python

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

Related

'EOFError: EOF when reading a line' while parsing argv in jupyter notebook

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

How to correct TypeError in unpacking script

I am learning to code Python 2 and have set up a proven, working script using "Learn Python the Hard Way" where in which I unpack an argument variable. The code is as follows:
from sys import argv
script, username = argv
prompt = '> '
print "Hi %s, I'm the &s script." % (username, script)
I expect the code to print:
Hi Zed, I'm the ex.py14 script.
Instead, this is the output:
Traceback (most recent call last):
File "ex14.py", line 6, in <module>
print "Hi %s, I'm the &s script." % (username, script)
TypeError: not all arguments converted during string formatting
Change the &s to %s in the print statement, please check the code. While string formatting its giving error.
from sys import argv
script, username = argv
prompt = '> '
print "Hi %s, I'm the &s script." % (username, script)
%s instead of &s
from sys import argv
script, username = argv
prompt = '> '
# %s instead of &s
print "Hi %s, I'm the %s script." % (username, script)

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

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