Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

Variable problems...

Sun Aug 12, 2012 3:52 pm

I am making a simple 7 segment calculator using the python 2.7 and the RPi.GPIO libary... However I have been having some problems when it comes to naming variables. I have 2 variables in the program, 1 for each digit I want to add together. The first variable is called 'dig' and the second is called 'number'... Here is the code after the variable name:

number = int(raw_input'enter the first number: '

When I run the module it just says syntax error and highlights the variable name. :(

I am a python 3 man myself however because the GPIO libary only works with python 2.7 I have had to downgrade. I have had similar problems in other code... Could someone explain what I have done wrong? Is it the variable name itself? Are variables different in python 2.7 than they are 3.1? I am very confused...

User avatar
croston
Posts: 707
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: Variable problems...

Sun Aug 12, 2012 4:11 pm

RPi.GPIO was designed, released and tested with Python 3 right from the start.

Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

Re: Variable problems...

Sun Aug 12, 2012 4:31 pm

OK, I got it working now and i don't want to change that... That still does not answer why I can't use a variable.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Variable problems...

Sun Aug 12, 2012 5:07 pm

@Matt1234: The place marked by a `SyntaxError` is the place where the compiler finally detected there is something wrong. This means the actual error may be before that place. BTW in that line are two additional syntax errors, both have to do with parenthesis. More precisely the lack of them in important places.

Code: Select all

while not self.asleep():
    sheep += 1

Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

Re: Variable problems...

Sun Aug 12, 2012 5:35 pm

Ok... I still don't see what the problem is... Here is the entire code. BTW I only have a one 7 segment display wired up so that is why the numbers only go up to 9. Also because I copy and pasted it most of the indentation is wrong. :) Thanks

import RPi.GPIO as gpio
import time
import random
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(21, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(22, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(10, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(9, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(11, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(7, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(8, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(25, gpio.OUT)

number = (int(raw_input('Second single digit number to add to first digit:\n '))

dig = (int(raw_input('First single digit number: '))


digiplus = int(dig + number)

if digiplus == 2
gpio.output(17, True), gpio.output(9, True), gpio.output(21, False), gpio.output(22, False), gpio.output(10, False), gpio.output(7, False), gpio.output(11, False)

elif digiplus == 4
gpio.output(21, True), gpio.output(10, True), gpio.output(22, True), gpio.output(7, True), gpio.output(11, True), gpio.output(17, False), gpio.output(9, False)

elif digiplus == 5
gpio.output(17, True), gpio.output(22, True), gpio.output(10, True), gpio.output(7, True), gpio.output(21, False), gpio.output(9, False), gpio.output(11, False)

elif digiplus == 6
gpio.output(21, True), gpio.output(17, True), gpio.output(22, True), gpio.output(7, True), gpio.output(11, True), gpio.output(10, False), gpio.output(9, False)

elif digiplus == 7
gpio.output(21, True), gpio.output(10, True), gpio.output(7, True), gpio.output(17, False), gpio.output(22, False), gpio.output(9, False), gpio.output(11, False)

elif digiplus == 8
gpio.output(21, True), gpio.output(17, True), gpio.output(22, True), gpio.output(7, True), gpio.output(11, True), gpio.output(9, True), gpio.output(10, True)

else digiplus == 9
gpio.output(21, True), gpio.output(17, True), gpio.output(22, True), gpio.output(7, True), gpio.output(11, True), gpio.output(9, False), gpio.output(10, True)import RPi.GPIO as gpio
import time
import random
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(21, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(22, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(10, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(9, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(11, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(7, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(8, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(25, gpio.OUT)

number = (int(raw_input('Second single digit number to add to first digit:\n '))

dig = (int(raw_input('First single digit number: '))


digiplus = int(dig + number)

if digiplus == 2
gpio.output(17, True), gpio.output(9, True), gpio.output(21, False), gpio.output(22, False), gpio.output(10, False), gpio.output(7, False), gpio.output(11, False)

elif digiplus == 4
gpio.output(21, True), gpio.output(10, True), gpio.output(22, True), gpio.output(7, True), gpio.output(11, True), gpio.output(17, False), gpio.output(9, False)

elif digiplus == 5
gpio.output(17, True), gpio.output(22, True), gpio.output(10, True), gpio.output(7, True), gpio.output(21, False), gpio.output(9, False), gpio.output(11, False)

elif digiplus == 6
gpio.output(21, True), gpio.output(17, True), gpio.output(22, True), gpio.output(7, True), gpio.output(11, True), gpio.output(10, False), gpio.output(9, False)

elif digiplus == 7
gpio.output(21, True), gpio.output(10, True), gpio.output(7, True), gpio.output(17, False), gpio.output(22, False), gpio.output(9, False), gpio.output(11, False)

elif digiplus == 8
gpio.output(21, True), gpio.output(17, True), gpio.output(22, True), gpio.output(7, True), gpio.output(11, True), gpio.output(9, True), gpio.output(10, True)

else digiplus == 9
gpio.output(21, True), gpio.output(17, True), gpio.output(22, True), gpio.output(7, True), gpio.output(11, True), gpio.output(9, False), gpio.output(10, True)

Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

Re: Variable problems...

Sun Aug 12, 2012 5:39 pm

Sorry, accidentally pasted it twice... Sorry

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Variable problems...

Sun Aug 12, 2012 6:49 pm

@Matt1234: Indentation is important in Python — so better use the code tags in your postings. There is a button labelled „Code” above the text field in the post edit form.

The syntax error is caused by unbalanced parenthesis in the code line befor the error. And the very same error is in the line where the compiler noticed that problem. You should really stop coding by copy and pasting. Learn about functions, loops, and datastructures and rewrite the program without that much copy and paste code.

All the ``if`` lines have a syntax error too.

And while making tuples of expressions without assigning and using the tuples is possible, it is bad style. Commas are not the syntax for separating statements in Python. That would be semicolons. Their use is discouraged though, because putting several statements into one line usually makes the code harder to read.

Code: Select all

while not self.asleep():
    sheep += 1

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: Variable problems...

Sun Aug 12, 2012 7:17 pm

you are short in the bracket dept.

Code: Select all

number = (int(raw_input('Second single digit number to add to first digit:\n ')))
Karl

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Variable problems...

Sun Aug 12, 2012 7:22 pm

@karl101: I would argue the opposite — there is one parenthesis too much. Or in your correction a pair that is not necessary at all.

Code: Select all

while not self.asleep():
    sheep += 1

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: Variable problems...

Sun Aug 12, 2012 7:22 pm

Also, look into the world of loops, eg:

Code: Select all

gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(21, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(22, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(10, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(9, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(11, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(7, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(8, gpio.OUT)
gpio.setmode(gpio.BCM)
gpio.setup(25, gpio.OUT)
becomes:

Code: Select all

things = [17,21,22, 10,9 11,7 ,8,25]
gpio.setmode(gpio.BCM)   # this only needs doing once
for i in things:
    gpio.setup(i, gpio.OUT)

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: Variable problems...

Sun Aug 12, 2012 7:26 pm

BlackJack wrote:@karl101: I would argue the opposite — there is one parenthesis too much. Or in your correction a pair that is not necessary at all.
eh? three opening brackets require three closing brackets. But yes, the bracket on the int is surplus.

Code: Select all

 number = int(raw_input('Second single digit number to add to first digit:\n '))

karl101
Posts: 68
Joined: Wed Jan 11, 2012 10:09 am

Re: Variable problems...

Sun Aug 12, 2012 7:43 pm

Also, more loops

Code: Select all

if digiplus == 2
gpio.output(17, True), gpio.output(9, True), gpio.output(21, False), gpio.output(22, False), gpio.output(10, False), gpio.output(7, False), gpio.output(11, False)
use dictionary's and lists

Code: Select all

dig = int(raw_input('First single digit number: '))
digi = {}
digi[1] = {21: False, 17: Flase, 22: True, 7: True, 11: True, 10: False, 9: False}
digi[2] = {21: True, 17: True, 22: True, 7: True, 11: True, 10: False, 9: False}
..etc...

for i in digi[dig]:
   print i, digi[dig][i]
   gpio.output(i, digi[dig][i])
Karl

KCarscadden
Posts: 56
Joined: Sat Jun 09, 2012 9:00 pm
Location: Canada

Re: Variable problems...

Sun Aug 12, 2012 7:49 pm

The int() is not required in the line 'digiplus = int( dig + number)'. 'digiplus = dig + number' will work as well.

The if statements require a colon after the conditional

if digiplus == 2:

The final 'else digiplus == 9' should either be 'else:' or 'elseif digiplus == 9:'.

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: Variable problems...

Sun Aug 12, 2012 8:22 pm

@karl101: ``digi[dig]`` looks bad. Make the ``for`` loop over key, value pairs instead and some more meaningful names wouldn't hurt:

Code: Select all

digit = int(raw_input('First single digit number: '))

digit2pin_states = {
    1: {21: False, 17: Flase, 22: True, 7: True, 11: True, 10: False, 9: False},
    2: {21: True, 17: True, 22: True, 7: True, 11: True, 10: False, 9: False},
    # ...
}
for pin, state in digit2pin_states[digit]:
    gpio.output(pin, state)

Code: Select all

while not self.asleep():
    sheep += 1

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Variable problems...

Mon Aug 13, 2012 12:44 pm

Also incidently, you only have to set this once :

Code: Select all

gpio.setmode(gpio.BCM)
...unless you wish to change the mode you wish to use (unlikely ;) )

Oooh - re-read the thread and Karl already pointed it out :oops:
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Matt1234
Posts: 33
Joined: Mon Aug 06, 2012 12:34 pm

Re: Variable problems...

Fri Aug 17, 2012 10:03 am

Thanks for all your great replies... I have sorted out the problem! Though Texy you say that I can change the 'mode' what would I change it from and then to? Why would I do it? And what benefit would I get from it? Thanks guys!!! :)

texy
Forum Moderator
Forum Moderator
Posts: 5161
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: Variable problems...

Fri Aug 17, 2012 12:29 pm

You wouldn't want or need to that's why I said 'unlikely' ;)
T.
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Return to “Python”