python - 我不断收到异常的语法错误

标签 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()

我想知道为什么我不断从中得到语法错误。看起来不错,但我不断收到错误消息

最佳答案

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

关于python - 我不断收到异常的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43713699/

相关文章:

JavaScript 省略号 (...) 和冒号 (:)

python - 在 Python 中,如何从包含多个类的文件中导入一个类?

python - 有没有好的方法将python程序拆分成独立的模块?

function - 如果在数据库中找不到每个输入的特定错误消息

php - 如何在 PHP 中捕获包含文件中的语法错误?

python - Flask,Python - 增加 Heroku 应用程序的超时长度

python - Pyqt5 - 网格布局行为不当

python - 有没有比这更好的方法来编写 "depend on parameters"的 Python 函数?

javascript - PythonJS 本地服务器

php - "double underscore"和 "underscore x"有什么区别?