本文介绍了我不知道为什么这段代码中有语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这个要在我的Mac终端中运行的python代码,但是当我尝试在终端中使用python运行它时,它在第27行给了我一个语法错误,如下所示:
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:’)
^
我一直在尝试并首先弄清楚为什么会出现语法错误,也许是很简单的,我只是不赞成而已.该代码应该用于显示listeners数组各部分的读数.
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
推荐答案
self.data = raw_input(‘Enter Pressure,Temperature,WindDirection:’)
应该是
self.data = raw_input('Enter Pressure,Temperature,WindDirection:')
或
self.data = raw_input("Enter Pressure,Temperature,WindDirection:")
您使用了错误的引号.
这篇关于我不知道为什么这段代码中有语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!