I have a question regarding Python replace function!

Asked 2 years ago, Updated 2 years ago, 13 views

for answer in QF:
        ans = "{:.2f}".format(answer)
        if ans in "0.00j":
            print(ans.replace("0.00j", ""))
        else:
            print(ans)

If there is 0.00j in the answer, I have to delete it and print it out, so I used the replace function You keep saying

Calculated answer is(are): -3.73+0.00j -0.27-0.00j

This is how 0.00j is printed. What's the problem? Python masters, help!

python

2022-09-20 22:19

1 Answers

The third line, if an "0.00j": is if an "0.00j" in an: Please change it to

You used the replace function normally, but it is a matter of condition that the function is not operated.

So the overall code should be changed as below

for answer in QF:
        ans = "{:.2f}".format(answer)
        if "0.00j" in ans:
            print(ans.replace("0.00j", ""))
        else:
            print(ans)


2022-09-20 22:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.