[英]how do i run multiple lines of python in html
我試圖弄清楚如何從 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>
output 是:
print() print("選擇要執行的操作的對應數字或單詞:") print("鍵入 '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("輸入第一個數字:")) addend2 = float(input("輸入第二個數字:")) print("結果是", str(addend1 + addend2)) # --- SUBTRACT --- # elif operation == "2" or operation == "subtract": number1 = int(input("輸入第一個數字")) number2 = int(input("輸入第二個數字")) print("差異是" , str(number1 - number2)) # --- MULTIPLY --- # elif operation == "3" or operation == "multiply": multiplicand = float(input("Enter The First Number: ")) multiplier = float (input("輸入第二個數字:")) print("積是", str(multiplicand * multiplier)) # --- DIVIDE --- # elif operation == "4" or operation == "divide" : 股息 = float(input("輸入第一個數字 : ")) divisor = float(input("輸入第二個數字:")) print("結果是", str(dividend / divisor))
請注意,我復制並粘貼了 output,它寫在網站上的一行上
我不知道我做得很好,因為這是我第一次使用“pyscript”,所以如果你能解釋一些我找不到的東西,我會很高興
您正在尋找“PyScript”。 HTML 標簽內的 Python 代碼
您的示例不包括 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()
我寫了 18篇關於 PyScript 的文章,可能會幫助您入門。 大量帶有解釋的示例代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.