'How do I turn print into <p>
This is my script:
<script>
import random
import string
def get_random_string(length):
# choose from all lowercase letter
letters = string.printable
result_str = ''.join(random.choice(letters) for i in range(length))
print("Random string of length", length, "is:", result_str)
get_random_string(8)
get_random_string(6)
get_random_string(4)
</script>
I want to turn " print("Random string of length", length, "is:", result_str)" Into text in a paragraph in my website, How do I do so?
Solution 1:[1]
I dont really know html.... I need help with that also or just a way to convert it into js
If you just want to use javascript, in your html file like in your comment, then this is what I think you were trying to do:
function get_random_str(length) {
let result_str = '';
let characters = 'abcdefghijklmnopqrstuvwxyz';
let charactersLength = characters.length;
for ( let i = 0; i < length; i++ ) {
result_str += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result_str;
}
function display_text(elm, str) {
document.getElementById(elm).innerHTML = str;
}
display_text('first', get_random_str(8));
display_text('second', get_random_str(6));
display_text('third', get_random_str(4));
<p id="first">...</p>
<p id="second">...</p>
<p id="third">...</p>
If I misunderstood then my apologies
Solution 2:[2]
I found a javascript library that let's you do write basically python instead of javascript; it's called Brython at Brython website. Anyway, I bound your functions to some click event on some buttons. I used JSFiddle to run this sample. Hope it helps.
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js">
</script>
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.js">
</script>
</head>
<body onload="brython()">
<script type="text/python">
## https://brython.info/index.html
## Python in place of javascript
from browser import document, alert, console
from browser.widgets.dialog import InfoDialog
import random
import string
def get_random_string(ev):
# choose from all lowercase letter
length = ev.target.id
length = length.replace("button_rand","")
letters = string.printable
result_str = ''.join(random.choice(letters) for i in range(int(length)))
## Inspect your browser page and find console 'tab' at top or bottom...
console.log(result_str)
alert(result_str)
def hello(ev):
alert(ev.target.id)
document["button_alert"].bind("click", hello)
document["button_rand8"].bind("click", get_random_string)
document["button_rand6"].bind("click", get_random_string)
document["button_rand4"].bind("click", get_random_string)
</script>
<button id="button_alert">Say Hello</button>
<button id="button_rand8">Get random 8</button>
<button id="button_rand6">Get random 6</button>
<button id="button_rand4">Get random 4</button>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rolewest |
Solution 2 | houstonrahoyt |