我一直在异常时遇到语法错误

时间:2017-05-01 02:59:09

标签: python syntax error-handling syntax-error

main():
try:
    weight1 = int(input("Enter the weight of package one: "))
    weight2 = int(input("Enter the weight of package two: "))
    if weight1 or weight2 <= 0:
        raise ValueError('Invalid weight')
    price1 = float(input("Enter the price for package one: "))
    price2 = float(input("Enter the price for package one: "))
    math1 = weight1 / price1
    math2 = weight2 / price2
    if math1 < math2:
        print("Package 1 has two has the better price")
    else:
        print("Package 2 has the better price price")
    except ValueError as excpt:
        print(excpt)
        print('Please provide a valid weight. \n')
    except ZeroDivisionError:
        print('Invalid price entered.')

main()

我想知道为什么我会一直收到语法错误。它看起来不错,但我一直收到错误

2 个答案:

答案 0 :(得分:0)

您在def定义之前缺少main():

答案 1 :(得分:0)

def main():

  try:
      weight1 = int(input("Enter the weight of package one: "))
      weight2 = int(input("Enter the weight of package two: "))
      if weight1 or weight2 <= 0:
          raise ValueError('Invalid weight')
      price1 = float(input("Enter the price for package one: "))
      price2 = float(input("Enter the price for package one: "))
      math1 = weight1 / price1
      math2 = weight2 / price2
      if math1 < math2:
          print("Package 1 has two has the better price")
      else:
          print("Package 2 has the better price price")
  except ValueError as excpt:
    print(excpt)
    print('Please provide a valid weight. \n')
  except ZeroDivisionError:
    print('Invalid price entered.')


main()
1. Missing def before main().
2. improper Indentation.