Page 1 of 1

Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 10:52 am
by Pix-IE
Greetings!

I recently started testing Python and tried to go on with it by Raspberry Pi Education Manual, but now I'm stuck with the part where I'm seemingly supposed to create Python Shell program via LXTerminal's "nano" program.

I opened up via "nano" program the "factors.py" -file, which contained the following code:

Image

...and rewrote the code as the one in the middle of the following picture:

Image

Image source: http://pi.cs.man.ac.uk/download/Raspber ... Manual.pdf

However, when I tried to write "python factors.py" in the LXTerminal, pretty much nothing happens, although the manual seemingly states otherwise.

I was wondering if somebody would kindly lend me a helping hand and tell me what I did wrong, if I did? :) The version of Python I used was 3.2.3 and the version of Nano was 2.2.6, just to mention.

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 1:06 pm
by kusti8
Well I can't see the code so you probably want to fix that. Did nothing happen and it returned to the command prompt or did nothing happen and you had to quit it with CTRL-C?

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 3:04 pm
by DougieLawson
Even when I lift your broken URLs out of your post I get 403'd by Google on them.

Post the pics on http://imgur.com and the plain links on here (not in [IMG] tags).

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 3:37 pm
by Pix-IE
I see, my apologies. Although I think it will be enough if I just send the codes here, without pics.

The code above reads as

Code: Select all

for number in range(1, 51):
    print(number, ":", end=" ")
    for divisor in range(1, number+1):
        if number%divisor == 0:
            print(divisor, end=" ")
    print()
and the code below reads as

Code: Select all

for number in range(1, 51):
    factors = 0
    print(number, end=": ")
    for divisor in range(1, number+1):
        if number%divisor == 0:
            print(divisor, end=" ")
            factors+=1
    if (factors == 2):
        print("and is a prime number")
    else:
        print()
After I brought and rewrote the code to the Nano from the Idle.rc -file, I saved the code in DOS form by pressing Ctrl+0 and closed the nano by Ctrl+X. Then I wrote "python factors.py" in LXTerminal, just like the manual said.

After that, the following happens:

Code: Select all

File "factors.py", line 3
  print(number, end=": ")
                   ^
SyntaxError: invalid syntax
pi@raspberrypi ~ $
So, I assume nothing happened and it returned to the command prompt.

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 3:47 pm
by poing
Pix-IE wrote:

Code: Select all

for number in range(1, 51):
    factors = 0
    print(number, end=": ")
I think

Code: Select all

print(number, end=": ")
should read

Code: Select all

print(number, "end=: ")
but my Python is a bit rusty at the moment.

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 4:04 pm
by Sleep Mode zZ
The code in your second code box runs fine in Python 3 but gives the error in Python 2. The syntax used in the print statement must be something that is not supported by Python 2.

Edit: To run it with Python 3, type:

Code: Select all

python3 factors.py

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 4:22 pm
by Pix-IE
It seems that by writing "python3 factors.py" the situation has moved on a bit, but still, after I write it down, the following happens:

Code: Select all

pi@raspberrypi ~ $ python3 factors.py
  File "factors.py", line 7
    factors+=1
             ^
TabError: inconsistent use of tabs and spaces in indentation
pi@raspberrypi ~ $

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 4:30 pm
by Sleep Mode zZ
Your code example in the second code box ran fine on my computer. It used spaces for indentation. It seems that you now are using both spaces and tabs for indentation, and Python wants you to choose one or another and use it consistently. There might be tools for fixing these problems but your code is so short that you could manually correct it.

This is one of Python's true annoyances when copying code from other sources than your own.

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 7:15 pm
by Pix-IE
Holy moe, it worked! It seems just like Sleep Mode zZ mentioned, I had to choose the indentations either by Tab or Space, not both in the same code.

Thanks for the help you've given me, all of you! ^.^ Hopefully I can still develop in this area!

Re: Could somebody help a Python amateur out here, please?

Posted: Sat Feb 21, 2015 7:20 pm
by kusti8
Pix-IE wrote:Ah yes, I probably should have mentioned that when written to the Python 3 -program itself, both of the codes work just fine for me as well, like with the second code all the prime numbers etc. showing up on "Python Shell" -window.

However, according to the education manual, by writing down "python factors.py" (seemingly "python3 factors.py" in this case) in LXTerminal, I should be able to see the output in LXTerminal just like in Python Shell. I would like to do just that, but something prevents the output from showing up over there, and that's the problem I'd like to have solved.
So are you getting no output what so ever, not even an error. You need to do what Sleep Mode zZ said. Either use tabs or spaces, but not both. I ran into this and got it by simply doing a find and replace on some big code, but on smaller code you can do it manually.
What output are you getting in python shell?