You are asked to write a program that prints the source-code of the above program as its output.
To test your program, you can proceed as follows:
- Run your program and save the generated output to a file named
generated.py
- Run
generated.py
and examine its output.We assume that your initial Python program is in file
lab3_x.py
the following steps must be run in the PyCharm terminal:%python lab3_x.py > generated.py %python generated.py
Notice the output redirection
>
. It causes the output of programlab3_x.py
to be written to the filegenerated.py
. The output oflab3_x.py
is the python program (source code) that converts stuff. (You may open filegenerated.py
with Pycharm and check its contents!)
My questions are as follows:
>
do when written in the terminal? generated.py
is executed in terminal?lab3_x.py > generated.py
it opens up a new file named after generated.py
, but with nothing inside. Why is this?My code:
print('# Fahrenheit to Celsius conversion program\n')
print("fahren = float(input('Enter degrees Fahrenheit: '))")
print("celsius = (fahren - 32) * 5 / 9")
print("print(fahren, 'degrees Fahrenheit equals',")
print(" format(celsius.'.1f'), 'degrees Celsius')")
When you run your code, you are using redirection ">" which outputs the code to a file. So your print statements actually print to the python file, which should be executable assuming the print statements result in a correct python file.
So lab3_x.py > generated.py
will write the print statements onto the python file/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.