User avatar
Pix-IE
Posts: 10
Joined: Wed Feb 18, 2015 8:23 pm

Could somebody help a Python amateur out here, please?

Sat Feb 21, 2015 10:52 am

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.

User avatar
kusti8
Posts: 3441
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

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

Sat Feb 21, 2015 1:06 pm

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?
There are 10 types of people: those who understand binary and those who don't.

User avatar
DougieLawson
Posts: 34096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

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

Sat Feb 21, 2015 3:04 pm

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).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
Pix-IE
Posts: 10
Joined: Wed Feb 18, 2015 8:23 pm

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

Sat Feb 21, 2015 3:37 pm

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
[email protected] ~ $
So, I assume nothing happened and it returned to the command prompt.

poing
Posts: 1131
Joined: Thu Mar 08, 2012 3:32 pm

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

Sat Feb 21, 2015 3:47 pm

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.

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

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

Sat Feb 21, 2015 4:04 pm

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

User avatar
Pix-IE
Posts: 10
Joined: Wed Feb 18, 2015 8:23 pm

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

Sat Feb 21, 2015 4:22 pm

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

[email protected] ~ $ python3 factors.py
  File "factors.py", line 7
    factors+=1
             ^
TabError: inconsistent use of tabs and spaces in indentation
[email protected] ~ $

Sleep Mode zZ
Posts: 319
Joined: Sun Aug 19, 2012 5:56 am
Location: Finland

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

Sat Feb 21, 2015 4:30 pm

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.

User avatar
Pix-IE
Posts: 10
Joined: Wed Feb 18, 2015 8:23 pm

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

Sat Feb 21, 2015 7:15 pm

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!
Last edited by Pix-IE on Sat Feb 21, 2015 7:26 pm, edited 4 times in total.

User avatar
kusti8
Posts: 3441
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

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

Sat Feb 21, 2015 7:20 pm

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?
There are 10 types of people: those who understand binary and those who don't.

Return to “Python”