14 Replies - 914 Views - Last Post: 21 February 2013 - 06:12 AM Rate Topic: -----

#1 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Simple python program

Posted 13 February 2013 - 06:30 PM

Hi I'm trying to write a very simple program in python. It needs to read in an arbitrary number of strings from the command line and displays them sorted alphabetically. I'm not sure what the error is, but I keep getting one for the if statement. Can anyone help with this? Thanks


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";


Is This A Good Question/Topic? 0
  • +

Replies To: Simple python program

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: Simple python program

Posted 13 February 2013 - 07:12 PM

What's the error message? I see a lot of errors in your code¹, but none on the line with the if statement. That seems to be the one line that's actually correct. Also which version of Python are you using?

¹ Incidentally: are you aware that Python and Perl are not source-compatible?
Was This Post Helpful? 0
  • +
  • -

#3 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Simple python program

Posted 13 February 2013 - 07:57 PM

I'm not sure it is just saying this line has an error

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?
Was This Post Helpful? 0
  • +
  • -

#4 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Simple python program

Posted 13 February 2013 - 08:21 PM

?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: Simple python program

Posted 13 February 2013 - 08:35 PM

View Postferguson32, on 14 February 2013 - 03:57 AM, said:

I'm not sure it is just saying this line has an error

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

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?


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.

View Postferguson32, on 14 February 2013 - 04:21 AM, said:

?


!

This post has been edited by sepp2k: 13 February 2013 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Simple python program

Posted 14 February 2013 - 05:21 AM

Ok I changed it to this

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;

Was This Post Helpful? 0
  • +
  • -

#7 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: Simple python program

Posted 14 February 2013 - 07:51 AM

sys.argv is a list, 1 is a number. As the error message is telling you, you can't use + with a list as one operand and a number as the other. Either both operands have to be lists (concatenating the two lists) or both operands have to be numbers (adding the numbers).

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.
Was This Post Helpful? 1
  • +
  • -

#8 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: Simple python program

Posted 14 February 2013 - 07:57 AM

Well the error is pretty clear.
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?)
Was This Post Helpful? 0
  • +
  • -

#9 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Simple python program

Posted 14 February 2013 - 09:57 AM

Thanks Mekire, I'm trying to run it from the command line but it doesn't seem to be working.

I was trying to execute this pathway C:\Python33\sort.py is this incorrect?
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3352
  • View blog
  • Posts: 11,343
  • Joined: 12-December 12

Re: Simple python program

Posted 14 February 2013 - 11:18 AM

View Postferguson32, on 14 February 2013 - 09:57 AM, said:

Thanks Mekire, I'm trying to run it from the command line but it doesn't seem to be working.

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.
Was This Post Helpful? 0
  • +
  • -

#11 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Simple python program

Posted 15 February 2013 - 05:48 PM

can you point me to a tutorial because none of the sites are showing me how to do it correctly? Wouldn't you just enter the pathway?
Was This Post Helpful? 0
  • +
  • -

#12 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple python program

Posted 15 February 2013 - 06:22 PM

You would need to invoke the Python interpreter to run the .py file:
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

Was This Post Helpful? 0
  • +
  • -

#13 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 183
  • Joined: 29-May 12

Re: Simple python program

Posted 16 February 2013 - 06:16 AM

Yes I did, thank you. I'm still having some difficulty getting it to run though. This is the path C:\Python33\sort.py

I entered this python <Python33>\<sort>.py

It's saying unexpected error after line continuation character (pointing to the y from .py)
Was This Post Helpful? 0
  • +
  • -

#14 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Simple python program

Posted 16 February 2013 - 08:38 AM

Take the triangular brackets out, I just used them to denote variables where your path and file goes try:
python Python33\sort.py

Was This Post Helpful? 1
  • +
  • -

#15 lisperati  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 16-February 13

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1