neteng
Posts: 24
Joined: Wed May 11, 2016 3:56 am

Python 3 Coding Issue_Print Command

Wed Sep 28, 2016 11:04 pm

Hi,

my print command doesn't seem to work....I am using the nanpy-firmware to establish master-slave protocol with my arduino....when I apply hashtags to all the print commands, the program executes......when i don't it pops up a syntax error

the error is:

File"buttonLED.py", line 13
print("Failed to connect ot arduino")
^
SyntaxError: invalid character in identifier.

this was in a youtube tutorial....https://www.youtube.com/watch?v=QumIhvYtRKQ

the code is below:

Code: Select all

from nanpy import (ArduinoApi, SerialManager)
from time import sleep

ledPin = 7
buttonPin = 8
ledState = False
buttonState = 0

try:
    connection = SerialManager()
    a = ArduinoApi(connection = connection)
except:
    print(¨Failed to connect to arduino¨)

#setup the pinmodes as if we were in ARDUINO IDE
a.pinMode(ledPin, a.OUTPUT)
a.pinMode(buttonPin,a.INPUT)

try:
    while True:
        buttonState = a.digitalRead(buttonPin)
        print(¨Our button state is: {}¨.format(buttonState))
        if buttonState:
            if ledState:
                a.digitalWrite(ledPin, a.LOW)
                ledState = False
                print(¨LED OFF¨)
                sleep(1)
            else:
                a.digitalWrite(ledPin, a.HIGH)
                ledState = True
                print(¨LED ON¨)
                sleep(1)
except:
    a.digitalWrite(ledPin, a.LOW)

User avatar
rpdom
Posts: 15353
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Python 3 Coding Issue_Print Command

Thu Sep 29, 2016 4:52 am

It looks like you've got some weir quotation marks on that line

Code: Select all

    print(¨Failed to connect to arduino¨)
it should look like this

Code: Select all

    print("Failed to connect to arduino")
It is probably a result of a copy/paste from a web page with some bad formatting.

neteng
Posts: 24
Joined: Wed May 11, 2016 3:56 am

Re: Python 3 Coding Issue_Print Command

Thu Sep 29, 2016 8:50 am

rpdom wrote:It looks like you've got some weir quotation marks on that line

Code: Select all

    print(¨Failed to connect to arduino¨)
it should look like this

Code: Select all

    print("Failed to connect to arduino")
It is probably a result of a copy/paste from a web page with some bad formatting.
Hi rpdom....

No, i typed it in myself. When I hit the double quotation on the keyboard, thats the symbol that pops up. it does look weird. Even the print function which has its font of purple colour is not orange like the import function.....

Note that it happens after i do "sudo apt-get upgrade"...after I install the raspbian os and before the upgrade, it works fine...

What could be the problem??....is it my keyboard settings??...What should be the keyboard setting???

scotty101
Posts: 3732
Joined: Fri Jun 08, 2012 6:03 pm

Re: Python 3 Coding Issue_Print Command

Thu Sep 29, 2016 8:58 am

Try single quotes instead

Code: Select all

print('Hello World')
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
B.Goode
Posts: 8820
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python 3 Coding Issue_Print Command

Thu Sep 29, 2016 9:26 am

I don't know how you are getting this odd character or how to solve it, but just to point out that the issue has arisen in these forums before:

viewtopic.php?f=63&t=156285

User avatar
Paeryn
Posts: 2698
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Python 3 Coding Issue_Print Command

Thu Sep 29, 2016 9:58 am

It looks like you're getting a diaeresis rather than a quotation mark. I'd check your keyboard settings, if it's set to a language that uses diacritics then it may be thinking you are wanting to put it onto a letter (if you follow it with a vowel then the diaeresis will likely be put on the vowel).
She who travels light — forgot something.

Return to “Beginners”