|
| 1 | +》》Write a Python program that does the following: |
| 2 | +1. In the command line, enter content. If you leave the input blank, read the contents of a question.txt file in UTF-8 encoding from the current directory and save it to the "text" string. If both your input and the contents of question.txt are empty, output "Input is empty" and end the program. |
| 3 | +2. Import the 'api' package; pass the content entered or read into api.code() function |
| 4 | +3. Strip off the first line from the returned code in a string by matching characters between two ``` in the api. code()'s return value using UTF-8 encoding, save this part to "code" and write it as code.py (if there is no existing file called `code.py` then create one; if already exists, replace its content) |
| 5 | +4. Run 'code.py' from current directory: If successful, capture the output into a string named result', and pass text, code, and result to api.code_check() function in this order (api.code_check(text, code, result)). Otherwise save error message of "code.py" execution failure as `result` then call api.code_error(code, result) passing arguments by same sequence |
| 6 | +5. Strip off the first line from ```...``` part found again and replace current content with it on 'code.py' (if does not exist create one; if exists overwrite its content). If no match is found output the returned value of last step. |
| 7 | +6. Repeat steps 4 & 5 for five times: After fifth execution, check whether code.py executed without error - print "Cannot be certain that program matches expected behavior." and end otherwise display message as per return from api.code_error() function (if it was called) or output contents in 'code.py' if no errors occurred. If the last operation resulted into an exception then terminate with a prompt saying: "Program cannot execute correctly" |
| 8 | + |
| 9 | +》 |
| 10 | +```python |
| 11 | +import os |
| 12 | +import subprocess |
| 13 | +import re |
| 14 | + |
| 15 | +def read_input(): |
| 16 | + user_input = input("Please enter your content: ") |
| 17 | + if not user_input: |
| 18 | + try: |
| 19 | + with open("question.txt", "r", encoding="utf-8") as file: |
| 20 | + user_input = file.read().strip() |
| 21 | + except FileNotFoundError: |
| 22 | + user_input = "" |
| 23 | + return user_input |
| 24 | + |
| 25 | +def extract_code(response): |
| 26 | + match = re.search(r"```(.*?)```", response, re.DOTALL) |
| 27 | + if match: |
| 28 | + code_lines = match.group(1).strip().split('\n')[1:] |
| 29 | + return '\n'.join(code_lines) |
| 30 | + return None |
| 31 | + |
| 32 | +def run_code(code_path): |
| 33 | + try: |
| 34 | + result = subprocess.check_output(["python", code_path], universal_newlines=True) |
| 35 | + except subprocess.CalledProcessError as e: |
| 36 | + result = str(e) |
| 37 | + return result |
| 38 | + |
| 39 | +def save_code(code): |
| 40 | + with open("code.py", "w", encoding="utf-8") as file: |
| 41 | + file.write(code) |
| 42 | + |
| 43 | +def main(): |
| 44 | + text = read_input() |
| 45 | + if not text: |
| 46 | + print("The input is empty") |
| 47 | + return |
| 48 | + |
| 49 | + import api |
| 50 | + response = api.code(text) |
| 51 | + code = extract_code(response) |
| 52 | + if not code: |
| 53 | + print("The code content was not matched") |
| 54 | + return |
| 55 | + |
| 56 | + save_code(code) |
| 57 | + result = run_code("code.py") |
| 58 | + |
| 59 | + for i in range(5): |
| 60 | + if "Error" in result: |
| 61 | + response = api.code_error(code, result) |
| 62 | + else: |
| 63 | + response = api.code_check(text, code, result) |
| 64 | + |
| 65 | + new_code = extract_code(response) |
| 66 | + if not new_code: |
| 67 | + print(response) |
| 68 | + return |
| 69 | + |
| 70 | + save_code(new_code) |
| 71 | + result = run_code("code.py") |
| 72 | + |
| 73 | + if "Error" in result: |
| 74 | + print("The program does not work properly") |
| 75 | + else: |
| 76 | + print("Not sure if the program is as expected") |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
| 80 | +``` |
| 81 | + |
| 82 | +This app implements the features you described. Make sure that the 'api' package is installed correctly and that the 'code', 'code_check', and 'code_error' functions are included in the package before running this program. |
0 commit comments