有没有办法将python代码放入javascript onclick函数中?

2024-04-29 07:52:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将一些python代码放入javascript函数中,以便在单击按钮时调用它。我只是想知道在javascript函数中是否有标记或某种内置方式来编写python代码? 我知道我可以通过请求函数打开一个文件或类似的东西来实现这一点,但是如果有一种方法可以将整个解决方案包含在一个文件中,那就太好了

这是我试图输入函数的代码:

user_input = input("input text here: ")
cipher_key = int(input("input cipher key here: "))
for x in user_input:
    if x == " ":
        print(x)
    else:
        ord_num = ord(x)
        alf_num = ord_num - 97 + cipher_key
        en_num = alf_num % 26 + 97
        print(chr(en_num))

Tags: 文件key函数代码inputherejavascript按钮
1条回答
网友
1楼 · 发布于 2024-04-29 07:52:23

这取决于你的环境;如果您正在编写一个node js程序,您可以按照此处所示How to execute an external program from within Node.js?来完成。如果您正在编写客户端代码(用于web浏览器),则不能

编辑

您的代码相对简单,因此可以将函数转换为js。假设您正在编写Nodejs代码:

const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("input text here: ", function(user_input) {
    rl.question("input cipher key here: ", function(cipher_key) {
        rl.close();
        cipher_key = parseInt(cipher_key)
        for (let i = 0; i < user_input.length(); i++) {
            const x = user_input[i];
            if (x === " ")
                process.stdout.write(x)
            else {
                const ord_num = x.charCodeAt(0)
                const alf_num = ord_num - 97 + cipher_key
                const en_num = alf_num % 26 + 97
                process.stdout.write(String.fromCharCode(en_num))

            }
        }
    });
});

相关问题 更多 >