Generate exercises in html with Python

An example about how to make an interactive exercise in javascript, using python. In the video you will see how it works.

import random

tmp = "Calcola l'interesse su 10.000 € al tasso del 7% per 1 anno."

def genex():
	s = {}
	C = s["Capitale"] = random.randrange(100, 1000, 100)
	r = s["tasso"] = random.randint(2,9)
	s["year_m_d"] = random.choice(["anni", "mesi", "giorni"])
	if s["year_m_d"] == "anni":
		t = s["tempo"] = random.randint(1, 5)
		s["interessi"] = C*r*t/100
	if s["year_m_d"] == "mesi":
		t = s["tempo"] = random.randint(1, 11)
		s["interessi"] = C*r*t/1200
	if s["year_m_d"] == "giorni":
		t = s["tempo"] = random.randint(1, 250)
		s["interessi"] = C*r*t/36500
	# s["sol"] = C*r*t/100
	s["final"] = ""
	if t > 1:
		s["final"] = "i"
	else:
		if s["year_m_d"] == "mesi":
			s["final"] = "e"
		else:
			s["final"] = "o"
	if s["interessi"] == int(s["interessi"]):
		s["interessi"] = int(s["interessi"])
	else:
		s["interessi"] = str(round(s["interessi"], 2))
		if len(s["interessi"].split(".")[1]) == 1:
			s["interessi"] = s["interessi"] + " " + s["interessi"] + "0"
		add = s["interessi"].replace(".", ",")
		s["interessi"] = s["interessi"] + " " + add

	return s

def printex():
	s = genex()
	s["year_m_d"] = s["year_m_d"][:-1]
	print("input(\"Calcola l'interesse su {Capitale} € al tasso del {tasso} % per {tempo} {year_m_d}{final}\", \"{interessi}\");".format(**s))



def show():
	print("""
<style>
	.fontbig {
		font-size: 1.5em;
	}
</style>
<body oncontextmenu="return false;">
<script>

function check(casella, giusta, num){
	// the solutions are good both for low and capital letters
	if (giusta.includes(casella.value.toLowerCase())){
		casella.style.background = 'yellow';
		return casella.value.includes(giusta);
	}
	else{
		casella.style.background = 'red';
	}
}

function print_it(parola){
	if (soluzioni.innerHTML.includes(parola)){
	}
	else {
    	soluzioni.innerHTML += " - " + parola;
	}
}

var countdom = 1;
function input(domanda, giusta){
	var dom_h2 = "<table style='background:#fcab41;'><td><p class='fontbig' style='color: blue'>" + countdom++ + " " + domanda;
	var part1 = dom_h2 + "<center><input id='casella' class='fontbig' type=text class='t1' placeholder='?...' onchange=\\"if (check(this,'";
	part1 += giusta + "')){print_it(this.value)};\\" /></center></p></table>";
	document.write(part1);
	}
	

x = document.getElementsByClassName('t1');
for (i of x){
    i.value = "";
}

</script><script>
		""")
	for x in range(10):
		printex()
	print("</script>")
show()

https://github.com/formazione/pythonquiz


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.