Python寫一個(gè)簡單的在線編輯器
直接奔入主題看下面pywebio
程序,實(shí)現(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('成功運(yùn)行程序') except Exception as e: ? ? put_text('運(yùn)行:錯(cuò)誤 at %s' % e)
主要使用了pywebio
程序,實(shí)現(xiàn)了Python的簡陋在線編輯器。
相對(duì)C++編輯器就比較復(fù)雜,需要調(diào)用g++.exe,可能在您的電腦上,就不見得能用了,需要把Dev-C++安裝到我這個(gè)位置,程序才可以運(yùn)行,您可以修改程序。
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('成功運(yùn)行程序') ? ? 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('運(yùn)行:錯(cuò)誤 at %s' % e)
運(yùn)行時(shí)會(huì)報(bào)一個(gè)錯(cuò),不用管它,跟咱們的程序無關(guān)。如果不是這個(gè)錯(cuò),那就是您的程序有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('成功運(yùn)行程序') ? ? 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('運(yùn)行:錯(cuò)誤 at %s' % e)
測試程序,直接運(yùn)行第一個(gè),輸入一段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)
點(diǎn)擊藍(lán)色按鈕,觀察控制臺(tái):
成功。
再測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; }
輸入測試程序并提交,觀察控制臺(tái):
控制臺(tái)上多了個(gè)大桃心,成功運(yùn)行。
到此這篇關(guān)于Python寫一個(gè)簡單的在線編輯器的文章就介紹到這了,更多相關(guān)Python寫線編輯器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python基礎(chǔ)之函數(shù)的定義和調(diào)用
這篇文章主要介紹了python函數(shù)的定義和調(diào)用,實(shí)例分析了Python中返回一個(gè)返回值與多個(gè)返回值的方法,需要的朋友可以參考下2021-10-10基于python實(shí)現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型
這篇文章主要介紹了基于python實(shí)現(xiàn)模擬數(shù)據(jù)結(jié)構(gòu)模型,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06django channels使用和配置及實(shí)現(xiàn)群聊
本文主要介紹了django channels使用和配置及實(shí)現(xiàn)群聊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05Python接口測試數(shù)據(jù)庫封裝實(shí)現(xiàn)原理
這篇文章主要介紹了Python接口測試數(shù)據(jù)庫封裝實(shí)現(xiàn)原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05