如何在html中运行多行python

roejwanj  于 2023-04-18  发布在  Python
关注(0)|答案(1)|浏览(141)

我正试图弄清楚如何在html中运行来自Python的多行代码
到目前为止,我有:

<!DOCTYPE html>
<html>

    <py-script>
        print()
        print("Select the corresponding number or word for the operation to perform:")
        print("Type 'quit' To Quit")
        print("1. ADD")
            
        operation = input().lower()
                
        # --- ADD --- #
        if operation == "1" or operation == "add":
            addend1 = float(input("Enter The First Number: "))
            addend2 = float(input("Enter The Second Number: "))
            print("The Result Is ", str(addend1 + addend2))                     
     <py-script>

</html>

输出为:

print() print("Select The Corresponding Number Or Word For The Operation To Perform:") print("Type 'quit' To Quit") print("1. ADD") print("2. SUBTRACT") print("3. MULTIPLY") print("4. DIVIDE") operation = input().lower() # --- ADD --- # if operation == "1" or operation == "add": addend1 = float(input("Enter The First Number: ")) addend2 = float(input("Enter The Second Number: ")) print("The Result Is ", str(addend1 + addend2)) # --- SUBTRACT --- # elif operation == "2" or operation == "subtract": number1 = int(input("Enter the first number")) number2 = int(input("Enter the second number")) print("The Difference Is ", str(number1 - number2)) # --- MULTIPLY --- # elif operation == "3" or operation == "multiply": multiplicand = float(input("Enter The First Number: ")) multiplier = float(input("Enter The Second Number: ")) print("The Product Is ", str(multiplicand * multiplier)) # --- DIVIDE --- # elif operation == "4" or operation == "divide": dividend = float(input("Enter The First Number: ")) divisor = float(input("Enter The Second Number: ")) print("The Result Is ", str(dividend / divisor))

请注意,我复制和粘贴的输出,它是写在一行的网站上
我不知道我做得那么好,因为这是我第一次使用'pyscript',所以如果你能解释一些我没有找到的东西,我会很饱。

c0vxltue

c0vxltue1#

您的示例不包含PyScript框架。请注意<head>标记中的两行。

<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
  </head>
  <body>
    <py-script>
# Insert Python code here ...
    </py-script>
  </body>
</html>

注意:您必须与浏览器DOM交互以获取用户输入。当编写PyScript时,您必须在浏览器界面上编写代码,而不是在命令shell界面上编写代码。某些Python API在浏览器中无法工作。
你不能这样使用代码。

operation = input().lower()

我在PyScript上写了18个articles,可能会帮助你入门。很多带有解释的示例代码。

相关问题