问题
So I have this python code that I'm to run in my mac's terminal but when I try running it with python in the terminal it gives me a syntax error on line 27 like so:
python /Users/ssddeveloper/Desktop/observer.py
File "/Users/ssddeveloper/Desktop/observer.py", line 27
self.data = raw_input(‘Enter Pressure,Temperature,WindDirection:’)
^
I have been trying and trying to figure out why theres a syntax error in the first place, maybe it's something really simple that I just don't but yea. The code is supposed to display the readings of each part of the listeners array.
# -*- coding: utf-8 -*-
class AbstractWeatherTower:
def register(self, listener):
raise NotImplementedError("Must subclass me")
def unregister(self, listener):
raise NotImplementedError("Must subclass me")
def notify_listeners(self, event):
raise NotImplementedError("Must subclass me")
class AbstractWeatherListener:
def __init__(self, name, subject):
self.name = name
tower.register(self)
def notify(self, event):
raise NotImplementedError("Must subclass me")
class WeatherTower(AbstractWeatherTower):
def __init__(self):
self.listeners = []
self.data = None
def getUserAction(self):
self.data = raw_input(‘Enter Pressure,Temperature,WindDirection:’)
return self.data
# Implement abstract Class AbstractSubject
def register(self, listener):
self.listeners.append(listener)
def unregister(self, listener):
self.listeners.remove(listener)
def notify_listeners(self, event):
for listener in self.listeners:
listener.notify(event)
class PressureListener(AbstractWeatherListener):
def notify(self, event):
print self.name, "Current Barometric Pressure is ", event.split(“,”)[0], "atms"
class TemperatureListener(AbstractWeatherListener):
def notify(self, event):
print self.name, "The Temperature is: ", event.split(",")[1], "degrees F"
class WindListener(AbstractWeatherListener):
def notify(self, event):
print self.name, "The Wind Direction is from the ", event.split(“,”)[2].capitalize()
if __name__=="__main__":
# make a subject object to spy on
tower = WeatherTower()
# register two listeners to monitor itT
listenerT = TemperatureListener("<listener T>", tower)
listenerP = PressureListener("<listener P>", tower)
listenerW = WindListener("<listener W>", tower)
# simulated event
tower.notify_listeners ("<event 1>")
# outputs:
# <listener T> received event <event 1>
# <listener P> received event <event 1>
# <listener W> received event <event 1>
action = tower.getUserAction()
tower.notify_listeners(action)
#Enter something to do:hello
# outputs:
# <listener T> received event hello
# <listener P> received event hello
# <listener W> received event hello
回答1:
self.data = raw_input(‘Enter Pressure,Temperature,WindDirection:’)
This should be;
self.data = raw_input('Enter Pressure,Temperature,WindDirection:')
or
self.data = raw_input("Enter Pressure,Temperature,WindDirection:")
You're using wrong quotes.
回答2:
Instead of ASCII single or double quotes you're using U+2018 and U+2019 here, hence the syntax error.
Wrong quotes here as well:
event.split(“,”)
回答3:
It's your quotes. They're funny. Delete them and enter normal ones.
来源:https://stackoverflow.com/questions/28101152/i-cant-figure-out-why-there-is-a-syntax-error-in-this-code