首页 > 解决方案 > Python noob,我不知道它为什么给出 SyntaxError: Invalid syntax

问题描述

正如标题所说,我在

temp_string = lines[1][equals_pos+2:] from datetime import date

而且我不知道如何解决它,或者有什么问题。

def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:] from datetime import date
from apscheduler.scheduler import Scheduler
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_f

标签: pythonsyntax-error

解决方案


导入语句应该在单独的行

from datetime import date
from apscheduler.scheduler import Scheduler
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_f

推荐阅读