我写了一些类,包括购物车、客户、订单和一些订单折扣函数。这是我代码的最后一行
anu = Customer('anu', 4500) # 1
user_cart = [Lineitem('apple', 7, 7), Lineitem('orange', 5, 6)] # 2
order1 = Order(anu, user_cart) # 2
print(type(joe)) # 4
print(order1) # 5
format() # 6
伙计们,我知道格式会因为没有传递任何参数而抛出一个错误。我想问你们为什么错误首先出现,如果它首先出现,那么其余的代码如何执行良好。我认为python解释器一直在执行代码,当它发现一个bug时,它会删除所有输出并抛出该错误。 这是我的输出
Traceback (most recent call last):
File "C:\Users\User\PyCharm Community Edition with Anaconda plugin 2019.3.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1434, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "C:\Users\User\PyCharm Community Edition with Anaconda plugin 2019.3.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "D:/programming/python/progs/my_programs/abc_module/abstract_classes.py", line 97, in <module>
format()
TypeError: format expected at least 1 argument, got 0
<class '__main__.Customer'>
Congratulations! your order was successfully processed.
Customer name: anu
Fidelity points: 4500 points
Order :-
product count: 2
product : apple
amount : 7 apple(s)
price per unit : 7$ per 1 apple
total price : 49$
product : orange
amount : 5 orange(s)
price per unit : 6$ per 1 orange
total price : 30$
subtotal : 79$
promotion : Fidelity Deal
discount amount : 24.88$
total : 54.115$
如果我猜对了,您的主要问题是为什么部分代码似乎在检测到错误之前运行?那是因为Python是一种解释的语言
除了加载文件时检测到的纯语法错误外,所有错误都是在Python解释器执行语句时检测到的
这意味着程序将一直运行,直到出现实际错误为止,因此执行初始化和打印的代码将一直运行,因为其中没有错误。然后Python解释器将检测到
format
没有任何参数的错误,并报告该错误至于为什么先打印错误,然后再打印程序输出,这是因为错误和正常输出被写入不同的“文件”。错误写入“标准错误”(
stderr
),正常输出写入“标准输出”(stdout
)IDE将到
stderr
和stdout
的输出捕获到不同的内部缓冲区,并且似乎只是在将输出写入stdout
之前将输出写入stderr
发生这种行为是因为出于性能原因,stdout是缓冲的,而stderr不是。这意味着在stdout(
print
指令)上写入某些内容时可能会有延迟,但在stderr上写入错误时不会有延迟,因此导致stdout写入在stderr之后发生有关更多详细信息,请参见this
如果不希望发生这种情况,请使用:
调用脚本时。这具有禁用标准输出缓冲的效果
编辑:我看到您在pycharm解释器上运行,在本例中,您可以通过在
run configuration
>;中添加-u
参数来为python解释器使用-u
参数interpreter options
字段相关问题 更多 >
编程相关推荐