English 中文(简体)
其它: 语法不正确
原标题:else: syntax is incorrect

我对 Python 有点新, 我正试图写入此脚本, 以取消 1 mb 以上的打印任务 。 ( 它检查大小的线条设置为 1 mb, 以确保它正常工作 ) 。 出于某种原因, 我最后的语句总是说它有无效的语法 。 我检查了是否所有括号都关闭了, 我找不到一个不匹配的配对 。 请告诉我为什么它无效? 您能否查看一下我的行 24 ( 全名 =... grep...) 来确认它的语法是否正确?

#! /usr/bin/python

import os

infile = open ( test.pl ,  r )

outfile = open( print.reportpython ,  w+ )

newfile = infile.readlines()

newfile.pop(0)

count = 0

firstline = newfile[0]

splitline = firstline.split()

currentuser = splitline[1]

currentuser = str(currentuser)

for line in newfile:

  newline = line.split()

  names = newline[1]

  size = int(newline[2])

  names = str(names)

  print names

  if names is currentuser:

    if size >= 1:

      os.popen ("cancel lab01-10292")

  fullname = os.popen("cat /etc/passwd |grep " + newline[1] + "cut -d :  -f5")

  count += 1

  print count

  else:

    print outfile.write ("(" + currentuser + ")")

    print outfile.write (" ")

    count = 0

    currentuser = names
问题回答

你这样做:

if foo:
  bar
baz
else:
  bomb

错误。 在 if 及其相应的 else 之间的所有线条必须缩进比 if 更深的地方, 例如 :

if foo:
  bar
  baz
else:
  bomb

else 与前一行的缩进相同,但前一行的语句没有 else 条款。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...