element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Experts, Learning and Guidance
  • Technologies
  • More
Experts, Learning and Guidance
Ask an Expert Forum BASIC translation to Python
  • Blog
  • Forum
  • Documents
  • Leaderboard
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Experts, Learning and Guidance to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Locked Locked
  • Replies 48 replies
  • Answers 24 answers
  • Subscribers 293 subscribers
  • Views 10111 views
  • Users 0 members are here
  • translation
  • python
Related
See a helpful answer?

Be sure to click 'more' and select 'suggest as answer'!

If you're the thread creator, be sure to click 'more' then 'Verify as Answer'!

This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

BASIC translation to Python

tommy2k19
tommy2k19 over 6 years ago

I have written a few very basic programs in Python but have not done much program translation.

My friend asked me to translate his heating program from Basic to Python.

Is there an easy way to do this? I haven't tried anything yet?

 

Tha Basic code is below:

 

1:PRINT"HLOSS85.1":PRINT"":GOTO 3

 

3:"N"INPUT"INPUT NOTES> ";B$(0):PRINT B$(0):GOTO 3

 

4:PRINT "":IF L>0 END

 

5:GOSUB 9:GOTO 104

 

9:PRINT"------------------------":RETURN

 

10:"=" PRINT=LPRINT:PRINT"":END

 

20"L"PRINT=PRINT:PAUSE "PO":END

 

50:"S" CLEAR:USING

 

60:DIM B$(0)*24

 

70:DIM S(6),W(6),G(6),V(0),Z(0),P(0),Q(0),O(0),K(0),N(0):N=1:A(27)=25:R=25:T=600

 

80:M=21:J=-4:U=5.6:A(30)=55:A(32)=3:GOSUB 9:GOTO 1

 

104:INPUT"INPUT OUTSIDE TEMP C ";J

 

105:PRINT"OUTSIDE TEMP ";J;" C

 

106:INPUT"INPUT ROOM TEMP C ";M

 

107:C=M-J

 

108:PRINT"PRINT TEMP ";M;" C":PRINT"TD = ";C;" C

 

114: GOSUB 9

 

115:"C"B$(0)="":INPUT"INPUT ROOM NAME ";B$(0)

 

116:IF B$(0)="" GOTO 115

 

117:IF B$(0)="*" GOTO 320

 

120:PRINT B$(0)

 

125:Y$="N":INPUT"SAME AS LAST ROOM? ";Y$;:IF Y$="Y" GOTO 252

 

130:S(5)=0:P=0:Q=0:B=0:O=0:K=0k:V=0:O(0)=0:K(0)=0:A=0:G(5)=0:W=0

 

140:USING:L=0:W=0:H=0:INPUT"INPUT ROOM LENGTH M ";L,"INPUT WIDTH M ";W,"INPUT HEIGHT M ";H

 

165:Z=LW:V=ZH:IF V=0 GOTO 140

 

175:PRINT"L=";L;"M W=";W;"M H=";H;"M

 

176:PRINT"AREA ";Z;" M2

 

177:PRINT"VOLUME ";V; M3

 

180:S=1

 

190:S(S)=LH:GOSUB 500

 

200:S(S)=WH:GOSUB 500

 

210:S(S)=S(1):GOSUB 500

 

220:S(S)=S(2):GOSUB 500

 

230:S(S)=LW:GOSUB 500

 

240:S(S)=S(5):GOSUB 500

 

245:USING:D=M-J:INPUT"INPUT AC/HR ";N;"INPUT TD C ";D

 

246:PRINT N;" A/C @ ";D;" C TD":B=.33VND

 

247:INPUT"INPUT SAFETY % ";A(33)

 

248:IF A(33)>100 GOTO 247

 

249:A(36)=INT B+INT P+INT Q

 

250:A(34)=INT((A(33)/100)*A(36))

 

251:PRINT"SAFETY ";A(33);"% ";INT A(34);"W

 

252:K=INT A(34)+INT A(36)

 

253:Y$="":INPUT"IS INFO CORRECT ? ";Y$

 

254:IF Y$="N" GOTO 115

 

255:IF Y$<>"Y" GOTO 253

 

256:Z(0)=S(5)+Z(0):P(0)=INT P+P(0):Q(0)=INT Q+Q(0):V(0)=INT B+V(0);O(0)=O+O(0)

 

257:N(0)=V+N(0):K(0)+K+K(0):A(35)=A(34)+A(35)

 

258:PRINT"- ROOM HEAT LOSS -

 

260:PRINT"FABRIC ";INT Q;"W":PRINT"WINDOW ";INT P;"W":PRINT" VENT ";INT B;"W

 

261:PRINT"SAFETY ";INT A(34);"W

 

262:PRINT"TOTAL ROOM LOSS ";K;"W

 

263:IF V=0 GOTO 265

 

264:PRINT INT ((K/V)+.5);" W/M3

 

265:GOSUB 9

 

266:"H"USING:INPUT"INPUT RADIATOR HT MM ";T

 

267:PRINT"RAD HEIGHT ";T;"MM

 

268:PRINT"SINGLE ";INT(544K/T);"MM LENGTH

 

269:PRINT"DOUBLE ";INT(362/T);"MM

 

270:GOSUB 9

 

310:GOTO 115

 

320:PRINT"

 

330:"G"USING:PRINT:PRINT"-------- TOTALS --------

 

350:PRINT"FLOOR AREA ";Z(0);" M2

 

355:PRINT"VOLUME ";N(0);" M3

 

410:PRINT"FABRIC LOSS ";INT Q(0);"W

 

420:PRINT"WINDOW LOSS ";INT P(0);"W

 

430:PRINT"VENT LOSS ";INT V(0);"W

 

440:PRINT"SAFETY LOSS ";INT A(35);"W

 

457:GOSUB 9

 

460;K=INT Q(0)+INT P(0)+INT V(0)+ INT A(35):PRINT "TOTAL LOSS ";INT K;"W

 

465:PRINT"AV.HEAT LOSS ";INT((K/M-J))+.5);"W/C

 

467:IF N(0)=0 GOTO 480

 

468:PRINT" ";INT((K/N(0)))+.5);"W/M3

 

470:PRINT USING"###.##";"G VALUE ";(K/(M-J))/N(0);" W/M3/C

 

480:USING:GOSUB 9

 

484:GOTO 900

 

489:A(31)=K+A(29)*1E3

 

490:INPUT"INPUT BOILER MARGIN % ";A(27)

 

491:A(28)=A(31)*A(27)/1E2

 

492:PRINT"BOILER MARGIN":PRINT A(27);"% = ";INT A(28);"W

 

493:GOSUB 9

 

494:PRINT" BOILER SIZE

 

495:PRINT USING"#######.#";" ";(A(31)+A(28))/1E3;"KW

 

496:USING:GOSUB 9

 

497:PRINT "":PRINT"

 

498:END

 

500:IF S<5 PRINT"WALL ";S

 

502:IF S=5 PRINT"CEILING/ROOF

 

504:IF S=6 PRINT"FLOOR

 

505:X=0:G(S)=0:C=M-J

 

507:INPUT"INPUT FABRIC U VALUE ";X:GOTO 509

 

508:GOTO 515

 

509:INPUT"INPUT TD C";C

 

510:GOSUB 700

 

512:IF X>0 GOTO 530

 

515:PRINT"NO LOSS

 

530:S=S+1:RETURN

 

700:IF S<6 INPUT"INPUT GLASS AREA M2 ";G(S)

 

702:IF G(S)>S(S) GOTO 700

 

705:W(S)=S(S)-G(S):IF W(S)=S(S) GOTO 710

 

707:IF S<5 LET A=A+S(S)

 

710:F=W(S)CX:PRINT W(S);"X";X;"X";C;"=";F;"W

 

712:Q=Q+F

 

715:IF S(S)=W(S) RETURN

 

717:INPUT"INPUT GLASS U VALUE ";U

 

800:IF S=5 PRINT"ROOFLIGHT":GOTO 805

 

802:PRINT"WINDOW ";S

 

805:G=G(S)UC

 

810:PRINT G(S);"X";U;"X";C;"=";G;"W

 

815:P=P+G:O=O+G(S)

 

820:K(0)=G(5)

 

830:RETURN

 

900:"D"USING:PRINT"D.H.WATER":INPUT"INPUT NO.OF PEOPLE ";I

 

905:IF I>0 PRINT 920

 

910:A(29)=0:PRINT"NONE":GOTO 489

 

920:INPUT"INPUT LITRE/PERSON ";R

 

930:PRINT I;"PEOPLE X ";R;"LITRE

 

940:INPUT"INPUT WARMUP - HRS ";A(32)

 

942:INPUT"INPUT WATER TD C ";A(30)

 

945:A(29)=4.2A(A30)IR/3600A(32)

 

950:PRINT A(32);"HR WARMUP @ ";A(30);"C TD

 

955:PRINT USING"#####.#";HOT WATER CYL";A(29);"KW

 

970:USING:GOTO 489

 

975:"X"USING:INPUT"INPUT FLOOR AREA M2";Z(0):PRINT Z(0)

 

976:INPUT"INPUT VOLUME M3 ";N(0):PRINT N(0)

 

977:INPUT"INPUT FABRIC W ";Q(0):PRINT Q(0)

 

978:INPUT"INPUT WINDOW W ";P(0):PRINT P(0)

 

979:INPUT"INPUT VENT W "V(0):PRINT V(0)

 

980:INPUT"INPUT SAFETY W ";A(35):PRINT A(35)

 

995:GOTO "G"

 

997:"Z"INPUT"INPUT W";K:PRINT K;"W

 

998:GOTO "H"

  • Cancel

Top Replies

  • jomoenginer
    jomoenginer over 6 years ago in reply to tommy2k19 +4 suggested
    There is no GOTO or GOSUB in Python. It is used in C but discouraged in most cases; it is quite common in BASIC. You can think of a GOTO as in an if-elif-else case or just replace the GOTO from BASIC with…
  • jomoenginer
    jomoenginer over 6 years ago in reply to Jan Cumps +4
    Jan Cumps wrote: If it’s a good friend, show this thread. If it is not a very good friend, show the thread then run away . Or it would completely offend the friend and create a negative view of the folks…
  • jc2048
    jc2048 over 6 years ago in reply to Jan Cumps +4 suggested
    The Sharp 1251 seems to have been sold by Tandy rebadged as the PC-3. There's a user manual here that includes a reference for their version of BASIC. https://ia601707.us.archive.org/13/items/PC-3_Owners_Manual_19xx_Tandy…
Parents
  • basic2python
    0 basic2python over 1 year ago

    I stumbled across this post 2 years ago, looking for automated BASIC to Python conversion for an old BASIC project.

    I couldn't find a suitable automated conversion. So I researched transpilers, decided it was more work that it was worth and shelved the project. (If you interested in writing your own transpiler/converter, see "If you are interested in writing your own transpiler" below).

    But, a lot has changed in the last 2 years. Fortunately, you don't need to write a transpiler or perform the translation yourself.

    I prompted ChatGPT's Code Copilot (based on GPT-4 6/28/2024) to convert the HLOSS85 program to Python.

    It provided surprisingly good answers:
    * it generated an outline, and initial program revision.
    * when prompted, it performed some basic data flow and code flow analysis, and identified code blocks.
    * it interpreted variables and code blocks and provided explanations.
    * it detected and fixed some errors without any prompting.
    * working interactively with it, it added more functionality, fixed bugs and clarified original program functionality.

    The bad news is that it did not provide a complete, correct program from the initial prompt. I think that's too much to expect.

    Anyone attempting to use ChatGPT (or similar LLM) will still need to be able to understand both the original and AI-generated code, and be able to debug both.

    But, the good news is that ChatGPT Code Copilot seems to be able to significantly accelerate the process of program conversion.

    If you are interested in writing your own transpiler, read on.

    I found some good info on writing transpilers and why there probably aren't very many for BASIC-to-Python conversion (tomassetti.me/.../)

    Here are some links about transpilers:

    Tomassetti: How to Write a Transpiler (tomassetti.me/.../ )
    Terence Parr: Language Translation using ANTLR (theantlrguy.atlassian.net/.../Language Translation Using ANTLR and StringTemplate)

    If you're unfamiliar with these topics, this is a gentle introduction to scanning, parsing, compiling and interpreting:

    Spivak's Let's Build a Simple Interpreter (ruslanspivak.com/.../)

    Here is an example TI84 BASIC-to-Python open source transpiler project, written in Python: TabulateJarl8's ti842py project (github.com/.../ti842py)

    Automated transpiling of unstructured BASIC to python is difficult because there are 2 problems to address:
    1. removing the GOTO statements and transforming the program into structured code, and
    2. translating the BASIC program to Python

    There are lots of resources for automating the removal of GOTO statements. Here are a few:
    * www.researchgate.net/.../2644650_A_Goto-Elimination_Method_And_Its_Implementation_For_The_McCat_C_Compiler
    * www.researchgate.net/.../2609386_Taming_Control_Flow_A_Structured_Approach_to_Eliminating_Goto_Statements
    * www.ndss-symposium.org/.../11_4_2.pdf
    * Aho, Lam, Sethi, Ullman 2006, aka, "the purple dragon book", ch. 9.7.3 Region-Based Analysis; T1-T2 Reduction algorithm

    If you are serious about writing a BASIC-to-Python conversion program yourself, you'll probably want to:
    a. parse the BASIC program into an abstract program representation
    b. remove GOTO statements, transforming the program into a structured program
    c. generate Python code from the abstract structured program

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Reply
  • basic2python
    0 basic2python over 1 year ago

    I stumbled across this post 2 years ago, looking for automated BASIC to Python conversion for an old BASIC project.

    I couldn't find a suitable automated conversion. So I researched transpilers, decided it was more work that it was worth and shelved the project. (If you interested in writing your own transpiler/converter, see "If you are interested in writing your own transpiler" below).

    But, a lot has changed in the last 2 years. Fortunately, you don't need to write a transpiler or perform the translation yourself.

    I prompted ChatGPT's Code Copilot (based on GPT-4 6/28/2024) to convert the HLOSS85 program to Python.

    It provided surprisingly good answers:
    * it generated an outline, and initial program revision.
    * when prompted, it performed some basic data flow and code flow analysis, and identified code blocks.
    * it interpreted variables and code blocks and provided explanations.
    * it detected and fixed some errors without any prompting.
    * working interactively with it, it added more functionality, fixed bugs and clarified original program functionality.

    The bad news is that it did not provide a complete, correct program from the initial prompt. I think that's too much to expect.

    Anyone attempting to use ChatGPT (or similar LLM) will still need to be able to understand both the original and AI-generated code, and be able to debug both.

    But, the good news is that ChatGPT Code Copilot seems to be able to significantly accelerate the process of program conversion.

    If you are interested in writing your own transpiler, read on.

    I found some good info on writing transpilers and why there probably aren't very many for BASIC-to-Python conversion (tomassetti.me/.../)

    Here are some links about transpilers:

    Tomassetti: How to Write a Transpiler (tomassetti.me/.../ )
    Terence Parr: Language Translation using ANTLR (theantlrguy.atlassian.net/.../Language Translation Using ANTLR and StringTemplate)

    If you're unfamiliar with these topics, this is a gentle introduction to scanning, parsing, compiling and interpreting:

    Spivak's Let's Build a Simple Interpreter (ruslanspivak.com/.../)

    Here is an example TI84 BASIC-to-Python open source transpiler project, written in Python: TabulateJarl8's ti842py project (github.com/.../ti842py)

    Automated transpiling of unstructured BASIC to python is difficult because there are 2 problems to address:
    1. removing the GOTO statements and transforming the program into structured code, and
    2. translating the BASIC program to Python

    There are lots of resources for automating the removal of GOTO statements. Here are a few:
    * www.researchgate.net/.../2644650_A_Goto-Elimination_Method_And_Its_Implementation_For_The_McCat_C_Compiler
    * www.researchgate.net/.../2609386_Taming_Control_Flow_A_Structured_Approach_to_Eliminating_Goto_Statements
    * www.ndss-symposium.org/.../11_4_2.pdf
    * Aho, Lam, Sethi, Ullman 2006, aka, "the purple dragon book", ch. 9.7.3 Region-Based Analysis; T1-T2 Reduction algorithm

    If you are serious about writing a BASIC-to-Python conversion program yourself, you'll probably want to:
    a. parse the BASIC program into an abstract program representation
    b. remove GOTO statements, transforming the program into a structured program
    c. generate Python code from the abstract structured program

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
Children
  • basic2python
    0 basic2python over 1 year ago in reply to basic2python

    Unfortunately, I could not figure out how to edit my previous post.

    2 of the links above were incorrectly constructed - the trailing parenthesis ')' was incorrectly appended to those URLs.

    The correct URLs are:

    Tomassetti: How to Write a Transpiler: https://tomassetti.me/how-to-write-a-transpiler/

    Parr: Language Translation Using ANTLR and StringTemplate: https://theantlrguy.atlassian.net/wiki/spaces/ST/pages/1409118/Language+Translation+Using+ANTLR+and+StringTemplate

    Spivak: Let's Build a Simple Interpreter: https://ruslanspivak.com/lsbasi-part1/

    TabulateJarl8: project ti842py: https://github.com/TabulateJarl8/ti842py

    "A Goto-Elimination Method And Its Implementation For The McCat C Compiler", Erosa: https://www.researchgate.net/publication/2644650_A_Goto-Elimination_Method_And_Its_Implementation_For_The_McCat_C_Compiler

    "Taming Control Flow: A Structured Approach to Eliminating Goto Statements", Erosa, Hendren: https://www.researchgate.net/publication/2609386_Taming_Control_Flow_A_Structured_Approach_to_Eliminating_Goto_Statements

    "No More Gotos", Yakdan, Eschweiler, Gerhards-Padilla, Smith: https://www.ndss-symposium.org/wp-content/uploads/2017/09/11_4_2.pdf

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube