How can I avoid NOBODY Runtime error? The following is sample code. This is a code that can be used for zero division error avoidance. Therefore, I know that it can not be used to avoid NOBODY error. But I can not find any other way. The following is a Runtime error message-> "IFELSE-VALUE expected input to be a TRUE / FALSE but got NOBODY instead." I appreciate your advice.
set top ifelse-value (nobody)
[ 0 ][ top ]
set ts turtles with [speed = 0 and not right-end]
set top max-one-of turtles [who]
set topx [xcor] of top ; A NOBODY error appears in "of" of this code
set L count ts with [xcor > topx]
The input of ifelse-value needs to be a reporter the returns either true or false (full details here. So, if you use nobody as the input, Netlogo does not evaluate whether or not the input is nobody or not, it just reads nobody- in other words your input is not returning either true or false.
For input, then, you need to instead use a boolean variable (one that is either true or false), a to-report that returns true or false, an expression that Netlogo can evaluate, etc. Consider the following examples:
to go
let x true
set top ifelse-value ( x )
["x is true"]
["x is NOT true"]
print ( word "Example 1: " top )
set top ifelse-value ( this-is-true )
["Reporter returned true"]
["Reporter did not return true"]
print ( word "Example 2: " top )
set x nobody
set top ifelse-value ( x = nobody )
["x IS nobody"]
["Is NOT nobody"]
print ( word "Example 3: " top )
set x 0
set top ifelse-value ( x = nobody )
["x IS nobody"]
["x Is NOT nobody"]
print ( word "Example 4: " top )
set top ifelse-value ( nobody = nobody )
["nobody = nobody"]
["nobody != nobody"]
print ( word "Example 5: " top )
end
to-report this-is-true
report true
end
Related
I'm practicing some basic coding, I'm running a simple math program running in the terminal on Visual Studio Code.
How do I create an option to return to the beginning of the program, or exit the program after getting caught in an if statement?
Example:
#beginning of program
user_input=input('Please select "this" or "that": ')
findings=user_input
If findings == this:
print(this)
# How can I redirect back to first user input question, instead
# of just ending here?
if findings == that:
print (that)
# Again, How do I redirect back to first user input, instead of
# the program ending here?
# Can I setup a Play_again here with options to return to user_input,
# or exit program? And then have all other If statements
# redirect here after completion? How would I do that? with
# another If? or with a for loop?
#end program
You can try wrapping the whole program in a while loop like this:
while(True):
user_input=input('Please select "this" or "that": ')
this = 'foo'
if user_input == this:
print(this)
continue
if user_input == this:
print(this)
continue
Unfortunately, that 'another technique' I thought of using didn't work.
Here's the code (the first example modified):
import sys
def main():
# Lots of setup code here.
def start_over():
return #Do nothing and continue from the next line
condition = 1==1 #Just a sample condition, replace it with "check(condition)".
float_condition = condition
def play(*just_define:bool):
if not just_define:
play_again = input('play again? "y" or "n"')
if play_again == 'y':
start_over() #Jump to the beginning of the prohram.
if play_again == 'n':
sys.exit() #Exit the program
while True:
if float_condition == True:
# print(float_condition)
play() #skip to play_again from here?
if float_condition == False:
#print(float_condition)
play() #skip to play_again from here?
#I removed the extra "main()" which was here because it'd cause an infinite loop-like error.
main()
Output:
play again? "y" or "n"y
play again? "y" or "n"n
Process finished with exit code 0
The * in the play(*just_define:bool) function makes the just_define parameter optional. Use the parameter if you want to only tell Python to search for this function, so it doesn't throw a ReferenceError and not execute anything that's after the line if not just_define:. How to call like so: play(just_define=True).
I've used nested functions. I've defined play so that you can call it from other places in your code.
Thanks to #Hack3r - I was finally able to choose to return back to the beginning of the program or exit out. But it resulted in a new issue. Now my print(results) are printing 4 or 5 times...
Here is the actual code I built and am working with:
def main():
math_Options=['Addition +','Subtraction -','Multiplication *','Division /']
math_func=['+','-','*','/']
for options in math_Options:
print(options)
print('Lets do some Math! What math function would you like to use? ')
while True:
my_Math_Function = input('Please make your choice from list above using the function symbol: ')
my_Number1=input('Please select your first number: ')
x=float(my_Number1)
print('Your 1st # is: ', x)
my_Number2=input('Please select your Second Number: ')
y=float(my_Number2)
print('Your 2nd # is: ', y)
z=float()
print('')
for Math_function in math_func:
if my_Math_Function == math_func[0]:
z=x+y
if my_Math_Function == math_func[1]:
z=x-y
if my_Math_Function == math_func[2]:
z=x*y
if my_Math_Function == math_func[3]:
z=x/y
if (z % 2) == 0 and z>0:
print(z, ' Is an EVEN POSITIVE Number')
if (z % 2) == 1 and z>0:
print(z, ' IS a ODD POSTIVE Number')
if (z % 2) == 0 and z<0:
print(z, ' Is an EVEN NEGATIVE Number')
if (z % 2) ==1 and z<0:
print(z, ' IS a ODD NEGATIVE Number')
if z==0:
print(z, 'Is is Equal to Zero')
print('')
play_again=input('Would you like to play again? "y" or "n" ')
if play_again == 'y':
continue
if play_again == 'n':
break
main()
main()
To play at Stretch the word, I've defined the following words, to try to work at the problem via the same method as this answer:
USING: kernel math sequences sequences.repeating ;
IN: stretch-words
! "bonobo" -> { "b" "bo" "bon" "bono" "bonob" "bonobo" }
: ascend-string ( string -- ascending-seqs )
dup length 1 + iota [ 0 swap pick subseq ] map
[ "" = not ] filter nip ;
! expected: "bonobo" -> "bonoobbooo"
! actual: "bonobo" -> "bbbooonnnooobbbooo"
: stretch-word ( string -- stretched )
dup ascend-string swap zip
[
dup first swap last
[ = ] curry [ dup ] dip count
repeat
] map last ;
stretch-word is supposed to repeat a character in a string by the number of times it's appeared up to that position in the string. However, my implementation is repeating all instances of the 1string it gets.
I have the feeling this is easily implementable in Factor, but I can't quite figure it out. How do I make this do what I want?
Hm... not a great golf, but it works...
First, I made a minor change to ascend-string so it leaves the string on the stack:
: ascend-string ( string -- string ascending-seqs )
dup length 1 + iota [ 0 swap pick subseq ] map
[ "" = not ] filter ;
So stretch-word can work like this:
: stretch-word ( string -- stretched )
ascend-string zip ! just zip them in the same order
[
first2 over ! first2 is the only golf I could make :/
[ = ] curry count ! same thing
swap <array> >string ! make an array of char size count and make it a string
] map concat ; ! so you have to join the pieces
Edit:
I think the problem was using repeat to do the job.
: ascend-string ( string -- seqs )
"" [ suffix ] { } accumulate*-as ;
: counts ( string -- counts )
dup ascend-string [ indices length ] { } 2map-as ;
: stretch-word ( string -- stretched )
[ counts ] keep [ <string> ] { } 2map-as concat ;
"bonobo" stretch-word print
bonoobbooo
indices length could also be [ = ] with count
guys. I created this procedure in NetLogo for my agents (farmers):
to calculate-deforestation
ask farmers [
set net-family-labor ( family-labor - ( ag-size * cell-labor-ag-keep ) )
set net-family-money ( family-money - ( ag-size * cell-cost-ag-keep ) )
ifelse net-family-labor < 0 or net-family-money < 0
[ set n-aband-cell-labor ( family-labor / cell-labor-ag-keep )
set n-aband-cell-money ( family-money / cell-cost-ag-keep )
set n-aband with-max [ n-aband-cell-labor n-aband-cell-money ]
]
[ set n-def-cell-labor ( net-family-labor / cell-labor-deforest )
set n-def-cell-money ( net-family-money / cell-cost-deforest )
set n-def with-min [ n-def-cell-labor n-def-cell-money ]
]
]
end
For the "n-aband", I would like to get the max value between "n-aband-cell-labor" and "n-aband-cell-money" (either one or the other; the same goes for "n-def"). I know a limited number of NetLogo primitives but the ones I was able to find do not work for my case, for instance, "with-max", "max-n-of", "max-one-of". I am sure there must be one that would work but I am having trouble finding it in the NetLogo dictionary. I wonder if anyone could suggest me one that could work for my case. Thank you in advance.
If you want to get the max value of a list, simply use max. So,
set n-aband max (list n-aband-cell-labor n-aband-cell-money )
will set n-aband to the highest of the two values.
I have made that loop my self and Iam trying to make it faster, better... but sometimes after it repeat searching for existing... it press random ( i think cuz its not similar to any img iam using in sikuli ) place on the screen. Maybe you will know why.
Part of this loop below
while surowiec_1:
if exists("1451060448708.png", 1) or exists("1451061746632.png", 1):
foo = [w_lewo, w_prawo, w_dol, w_gore]
randomListElement = foo[random.randint(0,len(foo)-1)]
click(randomListElement)
wait(3)
else:
if exists("1450930340868.png", 1 ):
click(hemp)
wait(1)
hemp = exists("1450930340868.png", 1)
elif exists("1451086210167.png", 1):
click(tree)
wait(1)
tree = exists("1451086210167.png", 1)
elif exists("1451022614047.png", 1 ):
hover("1451022614047.png")
click(flower)
flower = exists("1451022614047.png", 1)
elif exists("1451021823366.png", 1 ):
click(fish)
fish = exists("1451021823366.png")
elif exists("1451022083851.png", 1 ):
click(bigfish)
bigfish = exists("1451022083851.png", 1)
else:
foo = [w_lewo, w_prawo, w_dol, w_gore]
randomListElement = foo[random.randint(0,len(foo)-1)]
click(randomListElement)
wait(3)
I wonder if this is just program problem with img recognitions or I have made a mistake.
You call twice the exist method indending to get the same match (the first one in your if statement, the second time to assign it to the value. You ask sikuli to evaluate the image twice, and it can have different results.
From the method's documentation
the best match can be accessed using Region.getLastMatch() afterwards.
I have an excel output in the tab-delimited format:
temperature H2O CO2 N2 NH3
10 2.71539E+12 44374931376 7410673406 2570.560804
20 2.34216E+12 38494172272 6429230649 3148.699673
30 2.04242E+12 33759520581 5639029060 3856.866413
40 1.75491E+12 29172949817 4882467457 4724.305292
.
.
.
I need to convert these numbers to FORMAT(1X,F7.0,2X,1P4E11.3) readable for another code.
This is what I've come up with:
program fixformat
real temp, neuts(4)
integer i,j
character header
open(11,file='./unformatted.txt',status='old')
open(12,file='./formatted.txt',status='unknown')
read(11,*) header
write(12,*) header
do i = 1, 200
read(11,*) temp, (neuts(j),j=1,4)
write(12,23) temp, (neuts(j),j=1,4)
end do
23 FORMAT(1X,F7.0,2X,1P4E11.3)
close(11)
close(12)
return
end
I keep getting this error:
Fortran runtime error: Bad real number in item 1 of list input
Is there any other way to convert the data to that format?
You need a character string, not a single character for the header
character(80) header
other than that you program works for me. Make sure you have the right number of lines in your loop
Do i=1,200
Adjust 200 to the real number of your data lines.
If for some reason you still cannot read even a single line, you can also use the format:
read(11,'(f2.0,4(1x,f11.0))') temp, (neuts(j),j=1,4)
because the tab is just a character you can easily skip.
Notes:
Unformatted and formatted means something completely different in Fortran. Unformatted is what you may know as "binary".
Use some indentation and blank lines for your programs to make them readable.
There is no reason to explicitly use status=unknown. Just don't put anything there. In your case status=replace may be more appropriate.
The FORMAT statement is quite obsolete, in modern Fortran we use format strings:
write(12,'(1X,F7.0,2X,1P4E11.3)') temp, (neuts(j),j=1,4)
There is absolutely no reason for your return before the end. Returns is for early return from a procedure. Some put stop before the end program, but it is superfluous.
To read tab delimited data, I'd use a simple algorithm like the one below. NOTE: This is assuming that there is no tab character in any of your fields.
integer :: error_code, delim_index, line_index
character*500 :: data_line, field_data_string
double precision :: dp_value
Open(Unit=1001,File="C:\\MY\\PATH\\Data.txt")
DO
Read(UNIT=1001,End=106, FMT='(A)' ) data_line
line_length = LEN(TRIM(data_line))
delim_index = SCAN(data_line, achar(9) )
line_index = 0
DO WHILE ( delim_index .NE. 0 )
line_index = line_index + delim_index
IF (delim_index .EQ. 1 ) THEN ! found a NULL (no value), so skip
GOTO 101
END IF
field_data_string = data_line( (line_index-delim_index+1) : line_index )
READ( field_data_string, FMT=*, ERR=100) dp_value
PRINT *, "Is a double precision ", dp_value
GOTO 101
100 Continue
PRINT *, "Not a double precision"
101 Continue
IF ( (line_index+1) .GT. line_length ) THEN
GOTO 104 ! found end of line prematurely
END IF
delim_index = SCAN( data_line( line_index + 1 : ), achar(9) )
END DO
field_data_string = data_line( line_index + 1 : )
READ( field_data_string, FMT=*, ERR=102) dp_value
PRINT *, "Is a double precision ", dp_value
GOTO 103
102 Continue
PRINT *, "Not a double precision"
103 Continue
PRINT *, "Is a double precision ", dp_value
104 Continue
END DO
104 Continue
PRINT *, "Error opening file"
105 Continue
Close(1001)