I'm making a screensaver and I need to know what's wrong with my code.
GraphicsWindow.title="Screen Saver"
GraphicsWindow.Width=500
GraphicsWindow.Height=500
For i=1 To
Colour = GraphicsWindow.GetRandomColor()
GraphicsWindow.BrushColor=Colour
XCoord = Math.GetRandomNumber(1200)
YCoord = Math.GetRandomNumber(1200)
width=math.GetRandomNumber (300)
GraphicsWindow.Fillellipse(XCoord,YCoord,width,width)
Program.Delay(200)
EndFor
ContinueForEver = "Yes"
While ContinueForEver = "Yes"
EndWhile
I'm supposed to use for i=? to ? to make an infinite loop and I'm supposed to use While EndWhile for the continuation. So basically I'm supposed to make a screensaver which generates circles forever.
Something like this?
GraphicsWindow.title="Screen Saver"
GraphicsWindow.Width=500
GraphicsWindow.Height=500
While 1 = 1
Colour = GraphicsWindow.GetRandomColor()
GraphicsWindow.BrushColor=Colour
XCoord = Math.GetRandomNumber(1200)
YCoord = Math.GetRandomNumber(1200)
width=math.GetRandomNumber (300)
GraphicsWindow.Fillellipse(XCoord,YCoord,width,width)
Program.Delay(200)
EndWhile
You were very close. But you can not have a For loop without an end number like this:
For i = 1 to
You need to have an end number:
For i = 1 to 10 '<-- the loop will run 10 times
A While statement will run as long as its input is true. So in this case, as long as 1 = 1 the loop will continue (Which is forever).
Does that help? :D
Related
I'm having a little problem with my script here using a Logitech mouse. I will be using it for farming in a game.
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
for i = 0, 300 do
PressAndReleaseKey("f9")
Sleep(400)
PressAndReleaseKey("enter")
Sleep(600)
PressAndReleaseKey("f5")
Sleep(50)
PressMouseButton(1)
Sleep(50)
ReleaseMouseButton(1)
end
PressAndReleaseKey("1")
repeat
until IsMouseButtonPressed(3)
end
end
So it will loop for 300 times and then press 1 when it's done, then repeat the loop again for 300 times, so on & so on. Problem I'm facing is, when I'm trying to abort the script, it will first finish the for-loop before being stopped by using Right-click button(IsMouseButtonPressed(3)), which is really hard to time (300x is a lot)
How can I pause/stop it during the for-loop, would it be possible?
Frequently check if the button is pressed and break the loop.
Break up those long blocking Sleeps.
Instead of Sleep(400) consider doing something like
for i = 1, 400, 50 do
Sleep(50)
if IsMouseButtonPressed(3) then break end
end
You can use break to jump out of your for loop when IsMouseButtonPressed(3)
for i = 0, 300 do
if IsMouseButtonPressed(3) then
break -- exit for loop.
end
PressAndReleaseKey("f9")
Sleep(400)
PressAndReleaseKey("enter")
Sleep(600)
PressAndReleaseKey("f5")
Sleep(50)
PressMouseButton(1)
Sleep(50)
ReleaseMouseButton(1)
end
doing it like this means you can expect a maximum delay of 1.1 seconds due to the sleep calls, for the exit to be registered.
You could change your code by adding a function to poll IsMouseButtonPressed(3) during your sleep intervals.
local function MousePollingSleep(time)
loopCount = time / 50
for i = loopCount, 0, -1 do
if IsMouseButtonPressed(3) then
return false
end
sleepTime = (i >= 1 and 1 or i) * 50
Sleep(sleepTime)
end
return true
end
and change your for loop to
function OnEvent(event, arg)
if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
repeat
for i = 0, 300 do
PressAndReleaseKey("f9")
if MousePollingSleep(400) == false then break end
PressAndReleaseKey("enter")
if MousePollingSleep(600) == false then break end
PressAndReleaseKey("f5")
if MousePollingSleep(50) == false then break end
PressMouseButton(1)
if MousePollingSleep(50) == false then break end
ReleaseMouseButton(1)
end
ReleaseMouseButton(1)
PressAndReleaseKey("1")
until IsMouseButtonPressed(3)
end
end
I have been reading a text file a object and create a list with the contents.
textfile:
Activity Time Location
Football 8-9 Pitch
Basketball 9-10 Gym
Lunch 11-12 Home
Read 13-14 Library
Swim 14-15 Pool
openTime = 6
closeTime = 15
come = int(input('When do you want to come?'))
leave = int(input('When do you want to leave?'))
# endtime_of_activity and startTimeofactivity is equal to the startingtime
# of each activity and the end time of each activity in the textfile
# (taken from a list that I have been splitting).
for i in range(len(my_list)):
item = my_list[i]
if (i == 1):
continue
if closeTime <= come <= endtime_of_activity and startTimeofactivity < leave <= closeTime:
print(item.activities)
My question: As you can read in the textfile there are some activities appering on different times. For example football between 8 and 9. With the code I want to be able to skip the second element (basketball) as the code is doing, however, I want the if statement under "continue" to work. If i type that im coming 8 and leaving at 12 I want all the activities (excluding the second one) to show. This works for me when I'm doing a regular for-loop without skiping the second activity, like when im just writing: for i in my_list, then adding on the condition, but when Im doing the code above it shows me all the activites (except basketball) independeltly of when I chose to come and leave. What have I missed? How could I write the code better?
If you want to skip a certain activity, simply test if the activitiy to be printed is the same and skip it if it is:
with open("f.txt","w") as f:
f.write(("Activity Time Location\nFootball 8-9 "
"Pitch\nBasketball 9-10 Gym\nLunch 11-12"
" Home\nRead 13-14 Library\nSwim 14-15"))
data = []
with open("f.txt") as f:
for line in f:
act, time, what = (line.strip().split(" ") + ["","",""])[:3]
if data:
try:
time=list(map(int, time.split("-")))
except ValueError:
continue # invalid time: skip row
data.append([act,time,what])
header,*data = data
openTime = 6
closeTime = 15
come = 9
leave = 12
skip=set(["Basketball"])
fmt = "{:<15}"*len(header)
print(fmt.format(*header))
for act,(start,stop),what in data:
if act in skip:
continue
if start >= come and stop <= leave and openTime <= come and closeTime >= leave:
print(fmt.format(act, f"{start}-{stop}", what))
Output:
Activity Time Location
Football 8-9 Pitch
Lunch 11-12 Home
My code:
Func myFunc()
$lag = 1300
while (1)
MouseMove(870, 189)
sleep(10)
LC(870, 189)
sleep(1200 + $lag)
LC(1010,333)
sleep(100)
RC(826,115)
sleep(50)
LC(870,212)
sleep(50)
send("{ESC}")
sleep(150)
$x = 0
$y = 0
For $i = 0 To 27 Step 1
sleep(11)
MouseClick("left", 1158 + $x ,260 + $y)
$x+=42
if ($x = 168) Then
$x = 0
$y+=36
EndIf
Next
WEnd
EndFunc
The only delay within the for loop is the sleep(11) but it takes about .5 seconds (500ms) for each iteration rather than 11ms + whatever small delay. Also, completely removing the sleep(11) part of the loop still results in an approximately 500ms delay.
Incase anyone was wondering, it's a video game macro; the first part of the while loop opens an interface and sets something up while the second part (the for loop) is suppose to click through the inventory very quickly.
The mouse cursor takes time to move. Set the speed parameter to 0 to make it move instantly
MouseClick("left", 1158 + $x ,260 + $y,1,0)
I've got 53 variables named W1_C14_0 to W1_C14_52, and each has a value from 1 to 15. I need to find how many "spells" of each number there are in this list - i.e. how many seperate runs of 1's, 2's etc in each case. This is what I'm doing and it's working fine, but is there any way to condense it into a loop?
DO REPEAT first = W1_C14_0 to W1_C14_51 /
second = W1_C14_1 to W1_C14_52 .
DO IF (SYSMIS(first) OR first<>second) .
DO IF (second=1) .
COMPUTE W1_spells1 = W1_spells1 + 1 .
ELSE IF (second=2) .
COMPUTE W1_spells2 = W1_spells2 + 1 .
ELSE IF (second=3) .
COMPUTE W1_spells3 = W1_spells3 + 1 .
*and so on down to...
ELSE IF (second=15) .
COMPUTE W1_spells15 = W1_spells15 + 1 .
END IF.
END IF.
END REPEAT .
You can loop through the spell variables using the VECTOR command.
VECTOR W1_spells = W1_spells1 TO W1_spells15.
DO REPEAT first = W1_C14_0 to W1_C14_51 /
second = W1_C14_1 to W1_C14_52 .
DO IF (SYSMIS(first) OR first<>second).
LOOP #i=1 TO 15.
DO IF (second=#i) .
COMPUTE W1_spells(#i) = W1_spells(#i) + 1.
END IF.
END LOOP.
END IF.
END REPEAT.
import sgenrand
# program greeting
print("The purpose of this exercise is to enter a number of coin values")
print("that add up to a displayed target value.\n")
print("Enter coins values as 1-penny, 5-nickel, 10-dime,and 25-quarter.")
print("Hit return after the last entered coin value.")
print("--------------------")
#print("Enter coins that add up to 81 cents, one per line.")
total = 0
#prompt the user to start entering coin values that add up to 81
while True:
final_coin= sgenrand.randint(1,99)
print ("Enter coins that add up to", final_coin, "cents, on per line")
user_input = int(input("Enter first coin: "))
if user_input != 1 and user_input!=5 and user_input!=10 and user_input!=25:
print("invalid input")
total = total + user_input
while total != final_coin:
user_input = int(input("Enter next coin:"))
total = total + user_input
if user_input == input(" "):
break
if total > final_coin:
print("Sorry - total amount exceeds", (final_coin))
if total < final_coin:
print("Sorry - you only entered",(total))
if total== final_coin:
print("correct")
goagain= input("Try again (y/n)?:")
if goagain == "y":
if goagain == "n":
print("Thanks for playing ... goodbye!" )
I've been trying to create this loop, so it can repeat the whole program at the end when the user accepts/ if he accepts to do it again at the end.
I know you have to have a while statement around your whole program, but with my while true statement at the top, it only repeats the first part of my program and not the whole thing.
Well one thing you should be careful of is that you do not set the
total = 0
at the start of each loop. So when the user plays the game again. He will continue using his previous total. You should move total = 0 to the start of the loop
while True:
total = 0
Additionally, you need to deindent you first
while True
statement as it does not align properly with the rest of your code.
Finally, you need to allow the user to exit the while loop after he selects No for trying again.
if goagain == "n":
print("Thanks for playing ... goodbye!" )
break
This can be done by applying a break statement.