I'm trying to use subprocess
to generate a bunch of test cases, but something goes wrong.
Why is the following (simplified example) not working?
I have a first simple program sphere.py
:
#!/usr/bin/python
# -*- coding: utf-8 -*-
PI = 3.141592
radius = float(input( 'Give the radius of the sphere (in cm): ' ))
area = 4*PI*pow(radius, 2)
volume = 4/3*PI*pow(radius, 3)
print("\narea: {} cm²\nvolume: {} cm³".format(round(area,2),round(volume,2)))
Where an input of 4
results in (after running python3 sphere.py
):
area: 201.06 cm²
volume: 268.08 cm³
Then I have another file, generator.py
which runs the first one with subprocess
:
import subprocess
res = subprocess.run(["python", "sphere.py"], input="4",
capture_output=True, encoding="utf")
result_lines = res.stdout.split("\n")
for line in result_lines:
print(line)
But this results in (different volume, after running python3 generator.py
):
Give the radius of the sphere (in cm):
area: 201.06 cm²
volume: 201.06 cm³
Strangely, when I changed the volume
formula to 4.0/3.0*PI*pow(radius, 3)
it seemed to work fine...
What is going on here?
using Python 3.8