Python

Aus ProgrammingWiki

Wiki-Quelltext dieser Seite.

<language>Python</language>
Es wird der Python Interpreter Brython verwendet. Es wird eine ganze Reihe von Standard-Modulen mitgeliefert, die per import dynamisch geladen werden können. 

<run id="5dc551babe5c1">
import uuid
print(uuid.uuid4())
</run>

<run id="5dc559509f9d7">
import random
print(random.random())
</run>

<div id="jsOutputDiv"></div>
<run id="5e8dff97c3c5e">
jquery = window.jQuery
jquery("#jsOutputDiv").before('<button id="button_1">Click me!</button>')

window.alert("test")
</run>

Beispiel für Input und Output:

<run id="5dc55b74a2c73">
#Average Rainfall

#Start

yr = int(input('Enter how many years was tracked:'))

total = 0

for years in range(yr):
    print ('The number of months that was tracked was: {} \n'. format((yr)*12))
    for months in range(0,12):
        average = int(input('The average amount of rainfall for month {} year {}:'. format(months+1, years + 1)))
        print ('The average amount of rainfall for month {} year {} is {}.'. format(months+1, years + 1 , average))
        total = total + average
        
else:
    avgMonthly = total/(yr*12)
    print ('\n The average monthly rainfall for year {} is {} inches'. format(years + 1, avgMonthly))
    print ('\n The total amount of rainfall for year {} is {} inches.'. format(years + 1, total))
    
#End
</run>


<code id="5f612e924dbb9">
def my_function(x):
  return x + x
</code>
<check>
(my_function(99) == 198) and (my_function(18) == 36)
</check>
Es wird geprüft: (my_function(99) == 198) and (my_function(18) == 36)


== Turtle mit Python ==
<canvas></canvas>

<run id="639e291993a45">
canvas = window.Canvas
turtle = window.Turtle
c = canvas.new(1)
c.clear()
t = turtle.new(1)
t.home()

isUp = False
for i in range(18):
 t.penColor(i*10+50,0,250-10*i)  
 t.penWidth(8)
 t.forward(20)  
 t.right(20)
 isUp = not isUp 
 if (isUp):
   t.penUp() 
 else:
   t.penDown()
</run>