I can't figure out why there is a syntax error in this code

走远了吗. 提交于 2019-12-02 23:01:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!