PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, August 18, 2022

[FIXED] why my errors come first and print statements come second in python?

 August 18, 2022     output, python     No comments   

Issue

I wrote some classes which were a shopping cart, Customer, Order, and some functions for discounts for the orders. this is the final lines of my code.

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

guys, I know that format throws an error for not passing any argument I am asking you why the error comes first and if it comes first how does the rest of the code execute well. I think the python interpreter keeps executing code and when it finds a bug it deletes all the output and throws that error. this is my output.

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$

Solution

This behavior happens because stdout is buffered and stderr is not, for performance reasons. It means that there can be delay when you write something on stdout (print instruction) but not when an error is written on stderr, hence resulting in stdout writing occuring after stderr.

See this for more details.

If you don't want this to happen, use:

python -u myscript.py

when calling your script. This has the effect of disabling the buffering of stdout.

EDIT: I see that you run on pycharm interpreter, in this case you can use the -u parameter for the python interpreter by adding the -u argument in the run configuration > interpreter options field.



Answered By - LucG
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing