jeudi 1 avril 2021

Why is this if statement not fufilled when all the conditions are met?

private int getNextNodesDirection(Pair<Integer, Integer> current, Pair<Integer, Integer> nextNode, Robot target) {
    System.out.println("current x:" + current.getSecond());
    System.out.println("current y:" + current.getFirst());
    System.out.println("next x:" + nextNode.getSecond());
    System.out.println("next y:" + nextNode.getFirst());

    if (target.getCurrentDirection().equals(IWorld.Direction.UP)) {
        //if y increases and x stays the same
       
        if (nextNode.getFirst() > current.getFirst() && nextNode.getSecond().equals(current.getFirst())) {
            return 0;
        }
        // if x increases and y stays the same
        else if (nextNode.getSecond() > current.getSecond() && nextNode.getFirst().equals( current.getFirst())) {
            return 1;
        }




while (index < endIndex){
        int fSteps = 0;
        int direction = getNextNodesDirection(nodeList.get(index), nodeList.get(index+1), target);
        if (direction == 1){
            cost+=1;
            target.handleCommand(new RightCommand());
        }
        else if (direction == 2){
            cost+=2;
            target.handleCommand(new RightCommand());
            target.handleCommand(new RightCommand());
        }
        else if (direction == 3){
            cost+=1;
            target.handleCommand(new LeftCommand());
        }
        while (direction == 0){
            fSteps += 1;
            cost+=fSteps;
            index+=1;
            if(nodeList.get(index).getSecond() == (end.getX()) && nodeList.get(index).getFirst() == end.getY()){
                break;
            }
            direction = getNextNodesDirection(nodeList.get(index), nodeList.get(index+1), target);

So when the program runs it enters into getNextNodesDirection and executes as expected but on the second time round, although all the conditions are fulfilled it just goes passed the statement. Why? I would like it to return 0 while nextNode.getFirst() is greater than current.getFirst and nextNode.getSecond() is equal to current.getSecond()

I've screenshotted here in IntelliJ, while debugging, you can clearly see that y is increasing and x staying the same yet it passes the "fulfilled" condition and carries on. Why?

The language is Java 11, OS is Debian on a virtual machine.

Why does the if statement not recognize what is in the list?

I want the if statement to correctly compare what is in the list to what the input is in the enter_password variable.

user_password = []
password = input("Enter what you would like your password to be. \n ENTER: ")
user_password.append(password)
print(f"Password created! Your password is: {user_password}")
enter_password = input("Enter password: ")
if enter_password == user_password:
    print("Welcome")
else:
    print(f"{user_password} is wrong password")

Python - Problem with checking if element is in list

i came across a problem in Python and since i do this for fun and not as a professional i dont get why this does not work. I have a list that contains other lists with two numbers in each one. This function should check wether the element +1 and the element -1 are elements of my tp list. If yes, append them to stack. Code:

def check():
    tp = [[0, 1], [1, 1], [1, 2], [2, 2], [2, 3], [3, 3]]
    stack = []
    for i in tp:
        a = i[0]
        b = i[1]
        if [(a - 1), (b - 1)] and [(a + 1), (b + 1)] in tp:
            stack.append(i)
    return stack

Unfortunately the output is:

[[0, 1], [1, 1], [1, 2], [2, 2]]

[1, 2] is correct because [0, 1] and [2, 3] are elements of tp.

[2, 2] is correct because [1, 1] and [3, 3] are elements of tp.

Why does this function give me the other two also? For instance: first element of tp is [0,1] -> [-1,0] and [1,2] should be the calculated outputs but obviously [-1,0] is no in this list. Where is my (probably obvious) mistake? Thanks in advance.

Pipe a Windows Batch Scriptblock with existence test of variable to another command

I'm trying to get the following code to work that tests for existence of a variable to a pipe, which is a simplification of the real code I want to execute that contains other code inside the script block, but this demonstrates the problem:

( if defined some_variable echo ok ) | more
echo was unexpected at this time

It gives me the message "echo was unexpected at this time." Quoting the with parentheses after the "if" statement doesn't fix the problem, it just complains about the parentheses

( if defined some_variable ( echo ok ) ) | more
( was unexpected at this time.

Both variations of the "if" statement will work when not in the code block being piped.

if defined some_variable echo ok
ok
if defined some_variable ( echo ok )
ok

Additionally I can execute the code block but output to a file and that works to capture the output of the script block, particularly even with multiple lines of code in the script-block:

( if ok defined some_variable echo ok ) > some_text_file.txt

This style of "if" works inside the script-block-to-pipe structure :

( if 1==1 echo ok ) | more
ok
( if NOT 1==2 echo ok ) | more
ok

But I'm not understanding why the existence test with the 'defined' keyword completely bombs this piping structure.

My final goal is to get the following kind of code to work in batch script (which has more stuff it's doing than the sample below), but the problem boils down to the simplification mentioned at the beginning that bombs. The below code will work as a batch script to echo output to command prompt and log file, but variable checks in the script block destroy it.

( 
  echo some_stuff_like_a_program_header
  
  #test existence of a variable and if not defined earlier in script then echo some stuff
  if NOT defined some_variable then_alert_user_with_error_msg
  
  if some_variable==some_value (do_some_stuff) else (do_other_stuff)
  
) | powershell.exe -command '& { $input | tee-object -file out.log }'

IF ELSE statement inside the Query array - Wordpress

I've got the following query as a part of the function that shows posts in Buddypress user profile tab:

global $post, $bp;
$artist = bp_displayed_user_id();
$query_args = array(
            'post_type'   => 'article',
            'post_status' => array('publish', 'pending', 'draft'),
            'author'      => $artist,
            'tax_query'   => array(
                array(
                    'taxonomy' => 'magazine',
                    'field'    => 'slug',
                    'operator' => 'EXISTS',
                ),
            ),
);

I am trying to define the post status of the posts that will be showing to visitors. So if this is my own user profile and I am the author OR I am an administrator, then I should see all 3 post statuses 'publish', 'pending', 'draft'.

Else if I am a visitor to the user profile (not author, not admin), then I should only see posts that have a status 'publish'.

I was hoping to do define the $post_status outside the query like some of the other answers suggest and then call it to the query like this:

$post_status = ( bp_is_my_profile() || current_user_can('administrator') ) ? 'post_status' => array('publish', 'pending', 'draft') : 'post_status' => 'publish' );
    
$query_args = array(
                'post_type'   => 'article',
                'post_status' => $post_status,
                'author'      => $artist,
                'tax_query'   => array(
                    array(
                        'taxonomy' => 'magazine',
                        'field'    => 'slug',
                        'operator' => 'EXISTS',
                    ),
                ),
);

But that seems to be resulting in a fatal error.

What am I doing wrong here?

workflow of appending dictionary in nested loop [duplicate]

Can someone explain the workflow of this code.

dictionary in nested loop [{item: data[item] for item in data if item != '_id'} for data in documents] work.

how does appending of the dictionary work?

Java while loop help - nested if else statement

I am brand new to java and trying to create a story based around goldilocks and the three bears in which I ask the user a series of questions and the questions loop until the expected answer is given. It is for a class, and the expectation is we use while loops and if/ nested if else statements to do so. I have managed to get it set up so that the correct messages appear if the user inputs the wanted answer, however, haven't managed to get the question to loop if they input the wrong choice. Instead it just prints the error message and ends the program.

Can anyone give me any tips on where I am going wrong/ no exact fixes just a basic outline would be appreciated. Please assume that there is more to the program but I don't want to have anything flagged for plagiarism of myself later.

This is my if else statement so far (we are using GTerm https://jupiter.csit.rmit.edu.au/~e58140/GTerm/doc/GTerm.html hence gt.getInputString and so forth)

String porridge1;

porridge1 = gt.getInputString("How hot was the porridge? Cold, hot, somewhere in the middle?");
//dialog box asking user question about porridge
        gt.println("\n" + "How did Golidlocks find the porridge? Too cold, too hot, or just right?");
// printed message about porridge temperature for user to follow
        if (porridge1.equalsIgnoreCase("cold") || porridge1.equalsIgnoreCase(hot")) {
            gt.showErrorDialog("Oh no. This porridge was too " + porridge1 + "!");
//if user input = cold or hot; error dialog box appears
            gt.println("Oh no! This porridge was too " + porridge1 + "!");
//if user input = cold or hot; print error message
        } else {
            gt.showMessageDialog("This porridge is just the right temperature!");
//if user types anything else, dialog box confirming correct choice to appear
            gt.println("How wonderful! This porridge is just right!");
//if any other input given (middle, warm, perfect, just right etc, print following message