Python寫一個簡單的在線編輯器
直接奔入主題看下面pywebio
程序,實現(xiàn)了Python的簡陋在線編輯器
from pywebio.input import * from pywebio.output import put_text from os import system,remove try: ? ? code = textarea('Code Edit Online', code={'mode': "python",'theme': 'darcula'}, value='# input your code here\n') ? ? with open('temp.py','w',encoding = 'utf-8') as f: ? ? ? ? f.write(code) ? ? system('python temp.py') ? ? put_text('成功運行程序') except Exception as e: ? ? put_text('運行:錯誤 at %s' % e)
主要使用了pywebio
程序,實現(xiàn)了Python的簡陋在線編輯器。
相對C++編輯器就比較復雜,需要調(diào)用g++.exe,可能在您的電腦上,就不見得能用了,需要把Dev-C++安裝到我這個位置,程序才可以運行,您可以修改程序。
from pywebio.input import * from pywebio.output import put_text from sys import path from os import system,remove try: ? ? system('chcp 65001 >nul') ? ? code = textarea('Code Edit Online ( C++ )', code={'mode': "python",'theme': 'darcula',}, value='// input your code here.\n') ? ? with open('temp.cpp','w',encoding='utf-8') as f: ? ? ? ? f.write(code) ? ? put_text('成功運行程序') ? ? system(r'D:\Dev-C++\Dev-cpp\MinGW64\bin\g++.exe "%s\temp.cpp" -o "%s\temp.exe" && call "%s\temp.exe" && pause' % (path[0],path[0],path[0])) ? ? remove('temp.cpp') ? ? remove('temp.exe') except Exception as e: ? ? put_text('運行:錯誤 at %s' % e)
運行時會報一個錯,不用管它,跟咱們的程序無關。如果不是這個錯,那就是您的程序有BUG
。
from pywebio.input import * from pywebio.output import put_text from sys import path from os import system,remove try: ? ? system('chcp 65001 >nul') ? ? code = textarea('Code Edit Online ( C++ )', code={'mode': "python",'theme': 'darcula',}, value='// input your code here.\n') ? ? with open('temp.cpp','w',encoding='utf-8') as f: ? ? ? ? f.write(code) ? ? put_text('成功運行程序') ? ? system(r'D:\Dev-C++\Dev-cpp\MinGW64\bin\g++.exe "%s\temp.cpp" -o "%s\temp.exe" && call "%s\temp.exe" && pause' % (path[0],path[0],path[0])) ? ? remove('temp.cpp') ? ? remove('temp.exe') except Exception as e: ? ? put_text('運行:錯誤 at %s' % e)
測試程序,直接運行第一個,輸入一段Python代碼:
maxNumber = 100 numbers = [] min = 2 numberSum = 0 for i in range(1,101): ? ? numbers.append(i) ? ?? while min <= maxNumber: ? ? is_prime = True ? ? for i in range(2,min): ? ? ? ? if (min % i) == 0: ? ? ? ? ? ? is_prime = False ? ? ? ? ? ? break ? ? if is_prime == True: ? ? ? ? numbers.remove(min) ? ? min += 1 print(numbers)
點擊藍色按鈕,觀察控制臺:
成功。
再測C++,測試代碼如下(C++:畫桃心):
// Author:PanDaoxi #include <iostream> using namespace std; int main(){ ?? ?for(double y=1.5;y>-1.5;y-=0.1){ ?? ??? ?for(double x=-1.5;x<1.5;x+=0.05){ ?? ??? ??? ?double a=x*x+y*y-1; ?? ??? ??? ?char b=(a*a*a-x*x*y*y*y<=0.0?'*':' '); ?? ??? ??? ?cout<<b; ?? ??? ?} ?? ??? ?cout<<endl; ?? ?} ?? ? ?? ?return 0; }
輸入測試程序并提交,觀察控制臺:
控制臺上多了個大桃心,成功運行。
到此這篇關于Python寫一個簡單的在線編輯器的文章就介紹到這了,更多相關Python寫線編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于python實現(xiàn)模擬數(shù)據(jù)結構模型
這篇文章主要介紹了基于python實現(xiàn)模擬數(shù)據(jù)結構模型,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06django channels使用和配置及實現(xiàn)群聊
本文主要介紹了django channels使用和配置及實現(xiàn)群聊,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05Python接口測試數(shù)據(jù)庫封裝實現(xiàn)原理
這篇文章主要介紹了Python接口測試數(shù)據(jù)庫封裝實現(xiàn)原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05