num_args = ARGV + 1;
# Displays error if there are not at least three strings entered
if num_args < 3:
print "Error: There must be at least three strings"
exit;
#Reads the strings from the command line
my @a = @ARGV;
#Sorts the strings and the prints the sorted array
my @sorted = sort @a;
print "\n@sorted\n\n";
Simple python program
Page 1 of 114 Replies - 337 Views - Last Post: 21 February 2013 - 06:12 AM
#1
Simple python program
Posted 13 February 2013 - 06:30 PM
Replies To: Simple python program
#2
Re: Simple python program
Posted 13 February 2013 - 07:12 PM
¹ Incidentally: are you aware that Python and Perl are not source-compatible?
#3
Re: Simple python program
Posted 13 February 2013 - 07:57 PM
print "Error: There must be at least three strings"
It won't tell me the other errors after it until I fix this one. Can you tell me what my other errors are since it's such little code?
#5
Re: Simple python program
Posted 13 February 2013 - 08:35 PM
ferguson32, on 14 February 2013 - 03:57 AM, said:
print "Error: There must be at least three strings"
I'm pretty sure, the error message was not "There is an error on that line"?
Anyway, if you're using Python 3 (which I've asked about for a reason), print is a function and needs to be called with parentheses around its argument.
Quote
All your errors can pretty much be summarized as "you wrote Perl code and tried to execute it with a Python interpreter".
If you want to write Python code, you should learn the basics of Python. Things like how one creates and uses variables and how one calls functions.
ferguson32, on 14 February 2013 - 04:21 AM, said:
!
This post has been edited by sepp2k: 13 February 2013 - 08:39 PM
#6
Re: Simple python program
Posted 14 February 2013 - 05:21 AM
import sys
num_args = sys.argv + 1;
# Displays error if there are not at least three strings entered
if num_args < 3:
print ("Error: There must be at least three strings")
exit;
#Reads the strings from the command line
a = sys.argv;
#Sorts the strings and the prints the sorted array
sorted = sort(a);
print ("sorted")
It keeps saying there is an error at this line:
num_args = sys.argv + 1;
The error says builtins.TypeError: can only concatenate list (not "int") to list
File "C:\Python33\sort.py", line 10, in <module>
num_args = sys.argv + 1;
#7
Re: Simple python program
Posted 14 February 2013 - 07:51 AM
This is not Perl where arrays are converted to their length when used in a "scalar context". Python does not have such a thing as scalar contexts nor are there many occasions on which it performs implicit conversions.
PS: Python does not have a function called sort - only one called sorted. Lists also have a method called sort, which works in-place and does not have a meaningful return value.
#8
Re: Simple python program
Posted 14 February 2013 - 07:57 AM
sys.argv is a list and you are trying to add an int to it.
What are you expecting it to do?
This seems to be what you are trying to do, although perhaps you should read up on some python basics first:
import sys
myargs = sys.argv
num_args = len(myargs) + 1
if num_args < 3:
print ("Error: There must be at least three strings")
else:
myargs.sort()
print (a)
Lol... that was supposed to say print(myargs) at the end. Oops...
(No edit button on this forum?)
#9
Re: Simple python program
Posted 14 February 2013 - 09:57 AM
I was trying to execute this pathway C:\Python33\sort.py is this incorrect?
#10
Re: Simple python program
Posted 14 February 2013 - 11:18 AM
ferguson32, on 14 February 2013 - 09:57 AM, said:
I was trying to execute this pathway C:\Python33\sort.py is this incorrect?
Running Python from the command-line is normally covered in the first chapter of any decent Python tutorial. I suggest you take the time, as already recommended, to study Python fundamentals. There are probably tutorials that you can find here at DIC.
#11
Re: Simple python program
Posted 15 February 2013 - 05:48 PM
#12
Re: Simple python program
Posted 15 February 2013 - 06:22 PM
python <filelocation>/<file>.py
Or if you are in the same directory as the file you can just run:
python <file>.py
Is this what you meant to run it from the command line?
This post has been edited by Simown: 15 February 2013 - 06:22 PM
#13
Re: Simple python program
Posted 16 February 2013 - 06:16 AM
I entered this python <Python33>\<sort>.py
It's saying unexpected error after line continuation character (pointing to the y from .py)
#14
Re: Simple python program
Posted 16 February 2013 - 08:38 AM
python Python33\sort.py
#15
Re: Simple python program
Posted 21 February 2013 - 06:12 AM
import sys
if len(sys.argv) < 4:
print "There must be at least three strings."
sys.exit()
strings = sys.argv
strings.pop(0)
strings.sort()
print strings
One problem is that you need to check for 4 arguments cause the name of the program you are running is always the first argument in the list. Then you can pop the first argument ie. (the name of the program off the list) then invoke sort as argv returns a list object, and sort is a built in function to a list object. The print the list.
If your program name is inputs.py then from the console type "python inputs.py" assuming your running python or if your running windows,double click the icon. If your program in windows is saved a a *.py file your console will just pop up but if your program is *.pyw in windows nothing will pop up unless you've used a gui lib like pyqt or gtk.
|
|

New Topic/Question
Reply



MultiQuote






|