0

执行程序后,我收到以下语法错误,谢谢 thkang from this post

You entered:  ./#
Expanded to:  ./#
=================
File "./largestoddxyz.py", line 43
else: #x and z are even
   ^
SyntaxError: invalid syntax

这是代码:
我是使用 Python 的新手,并试图解决这个问题,但不幸的是,我没能做到。希望这里有人可以帮助我。

#!/usr/bin/env python
# This program exmamines variables x, y, and z 
# and prints the largest odd number among them

import sys

x,y,z = map(int,sys.argv[1:4])

if x%2 != 0:
  if y%2 != 0:
    if z%2 != 0:
      if x > y and x > z: #x is the biggest odd
        print 'x is the biggest odd ' and x
      elif y > z and y > x: #y is the biggest odd
        print 'y is the biggest odd ' and y
      elif z > x and z > y: #z is the biggest odd
        print 'z is the biggest odd ' and z

    else: #z is even
      if x > y: #x is the biggest odd
        print 'x is the biggest odd ' and x
      else: #y is the biggest odd
        print 'y is the biggest odd ' and y

  else: #y is even
      if z%2 != 0: #z is odd
        if x > z: #x is the biggest odd
          print 'x is the biggest odd ' and x
        else: #z is the biggest odd
          print 'z is the biggest odd ' and z
      else: #y,z are even and x is the biggest odd
        print 'x is the biggest odd ' and x

else: #x is even
  if y%2 != 0 and z%2 != 0: #y,z is odd
      if y > z: #y is the biggest odd
        print 'y is the biggest odd ' and y
      else: #z is the biggest odd
        print 'z is the biggest odd ' and z
  else: #x and y are even
    if z%2 != 0: #z is the biggest odd
      print 'z is the biggest odd ' and z
  else: #x and z are even
    if y%2 != 0: #y is odd
      if z%2 = 0: #z is even
        print 'y is the biggest odd ' and y

print 'finished'
4

1 回答 1

1

缩进错误;笔记

  else: #x and y are even
    if z%2 != 0: #z is the biggest odd
      print 'z is the biggest odd ' and z
  else: #x and z are even

你想缩进第二个else和它下面的所有东西。您可能想要运行pep8以验证一切是否正常,即您使用正确的间距并且不要混合制表符和空格字符。

此外,在行

if z%2 = 0: #z is even

你想要的是比较==,而不是分配=

于 2013-03-31T22:57:27.550 回答