简体   繁体   中英

how do i run multiple lines of python in html

I am trying to figure out how to run multiple lines of code from python in html

So far i have:

<!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>

The output is:

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 Diffrence 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))

Please note that i copy and pasted the output and it is writen on one line on the website

I don't know what i am doing that well because this is one of my first times using 'pyscript' so if you could explain something that i haven't been able to find i would be great full

You're looking for "PyScript". Python code inside HTML tags

Your example does not include the PyScript framework. Notice the two lines in the <head> tag.

<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>

Note:You must interface with the browser DOM to get user input. When writing PyScript you must code to the browser interface and not to a command shell interface. Some Python APIs will not work in the browser.

You cannot use code like this.

operation = input().lower()

I wrote 18 articles on PyScript that might help you get started. Lots of example code with explanations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM