from tkinter import *
import random
import time
def move(event):
if event.keysym == 'Up':
canvas.move(p, 0, -3)
elif event.keysym == 'Down':
canvas.move(p, 0, 3)
elif event.keysym == 'Left':
canvas.move(p, -3, 0)
elif event.keysym == 'Right':
canvas.move(p, 3, 0)
def mainloop():
while 1:
canvas.move(c, random.randint(-6, 6), random.randint(-6, 6))
tk.update_idletasks()
tk.update()
time.sleep(0.01)
colors=['black', 'white', 'red', 'yellow', 'blue', 'purple']
tk=Tk()
tk.geometry('400x400')
canvas=Canvas (tk, width=400, height=400)
canvas.pack(fill='both', expand=True)
p=canvas.create_rectangle(10, 10, 70, 150, fill=random.choice(colors), outline=random.choice(colors))
c=canvas.create_oval(190, 190, 210, 210, fill=random.choice(colors), outline=random.choice(colors))
canvas.bind_all('<KeyPress-Up>', move)
canvas.bind_all('<KeyPress-Down>', move)
canvas.bind_all('<KeyPress-Left>', move)
canvas.bind_all('<KeyPress-Right>', move)
mainloop()
Объяснение:
from tkinter import *
import random
import time
def move(event):
if event.keysym == 'Up':
canvas.move(p, 0, -3)
elif event.keysym == 'Down':
canvas.move(p, 0, 3)
elif event.keysym == 'Left':
canvas.move(p, -3, 0)
elif event.keysym == 'Right':
canvas.move(p, 3, 0)
def mainloop():
while 1:
canvas.move(c, random.randint(-6, 6), random.randint(-6, 6))
tk.update_idletasks()
tk.update()
time.sleep(0.01)
colors=['black', 'white', 'red', 'yellow', 'blue', 'purple']
tk=Tk()
tk.geometry('400x400')
canvas=Canvas (tk, width=400, height=400)
canvas.pack(fill='both', expand=True)
p=canvas.create_rectangle(10, 10, 70, 150, fill=random.choice(colors), outline=random.choice(colors))
c=canvas.create_oval(190, 190, 210, 210, fill=random.choice(colors), outline=random.choice(colors))
canvas.bind_all('<KeyPress-Up>', move)
canvas.bind_all('<KeyPress-Down>', move)
canvas.bind_all('<KeyPress-Left>', move)
canvas.bind_all('<KeyPress-Right>', move)
mainloop()
Объяснение:
// PascalABC.NET 3.1, сборка 1174 от 22.02.2016
begin
Writeln(Range(1,100).Select(i->sin(i)*cos(i)).Where(x->x<>0).Average)
end.
Тестовое решение:
-0.00136006072493969
2. А вот так учат писать это же школьные учителя:
// PascalABC.NET 3.1, сборка 1174 от 22.02.2016
var
m:array[1..100] of real;
i,k:integer;
s:real;
begin
s:=0;
k:=0;
for i:=1 to 100 do begin
m[i]:=sin(i)*cos(i);
if m[i]<>0 then begin
s:=s+m[i];
k:=k+1
end
end;
Writeln(s/k)
end.
Тестовое решение:
-0.00136006072493969