Перевод кода из pascal в python
Есть код на паскале и необходимо написать код, работающий также на python:
Попытка сделать такое на питоне не обвенчалась успехом:
![]()
Почти прямой аналог ввода программы на Паскале:
Как чаще делают в Python: (N я не стал убирать)
![]()
Дизайн сайта / логотип © 2023 Stack Exchange Inc; пользовательские материалы лицензированы в соответствии с CC BY-SA . rev 2023.3.11.43304
Нажимая «Принять все файлы cookie» вы соглашаетесь, что Stack Exchange может хранить файлы cookie на вашем устройстве и раскрывать информацию в соответствии с нашей Политикой в отношении файлов cookie.
Name already in use
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.txt
About
Simple code converter from pascal to python
Topics
Resources
Stars
Watchers
Forks
Releases
Packages 0
Languages
Footer
© 2023 GitHub, Inc.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Как перевести код с Pascal на Python?
Задача: Назовём нетривиальным делителем натурального числа его делитель, не равный единице и самому числу. Например, у числа 6 есть два нетривиальных делителя: 2 и 3. Найдите все натуральные числа, принадлежащие отрезку [289123456; 389123456] и имеющие ровно три нетривиальных делителя. Для каждого найденного числа запишите в ответе его наибольший нетривиальный делитель. Ответы расположите в порядке возрастания.
Например, в диапазоне [5; 16] ровно три различных натуральных делителя имеет число 16, поэтому для этого диапазона вывод на экране должна содержать следующие значения:
Вот решение на паскале, но как перевести данный код на питон. Я раньше на паскале программировал, но уже забыл его так как я уже перешел на новый язык программирования.
Вот попытка решить задачу, но у меня программа не работает. Выдает ошибку SyntaxError: invalid syntax
Transmogripy
Transmogripy is a tool to convert short pascal scripts to python. It is originally meant to convert TaLoS scripts but can be easily adapted to many other environments.
Requirements
Transmogripy is intentionally meant to be run in bare-bones environments, so the only requirements are python >= 3.6, it can even run in embeddable systems like QGIS’s python environment.
On Numpy
numpy is not a requirement, and is not even used. However, numpy might appear in the output script. This can be prevented by explicitly setting the allow_numpy parameter to False .
Usage
The main function is the convert function.
Behaviour of Result :=
There are many different ways to handle pascal’s Result:= code, and can be set with convert ‘s result_behaviour parameter:
- «variable» : a temporary variable __Return__ will be created and only returned at the end of the script
- «return» : Result:= will be converted to return and a check for an early return will not be made.
- «warn» : as «return» , except a warning will be issued if an early return is detected
- «try» (default): as «warn» , but if an early warning is detected, the conversion will be re-tried with the «variable» option
Comments and inline comments
The conversion attempts to convert comments, however, all inline comments (comments that have code after them in the same line) will be removed:
This behaviour can be changed with the remove_inline_comments parameter, setting it to False will instead raise an exception when an inline comment is encountered
Imports, headers, and segments
Some pascal function, like log , require python libraries to be imported. Transmorgopy automatically adds libraries that are referenced in the converted code.
In addition, additional lines can be added before/after the converted code using the pre_words / post_words parameters. (note: pre_words ‘s default is (from talos import *,) , resulting in the from talos import * line)
Code checking
By default, Transmogripy checks the syntax of the output script, and issues a warning if any errors are found. This can be changed by setting the check_syntax parameter to False .
Not Supported
The following Pascal/Delphi features are not supported and will raise an error: