lundi 1 août 2016

Local variable manipulated in if statement can't be accessed from within the scope. Why?

First, I calculate whether num is a power of 2.

If it is, then I set flag to true, otherwise, to false.

But the Problem is I can't access that flag from outside the condition.. You've gotta help me.. Thanks

public  static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a number to check if it's a POWER 2 : ");
    int num = scan.nextInt();     // input from user
    boolean flag = false;         // check to see if it is a power 2

    for(int i = 0; i < num/2;i++){
        if(num==Math.pow(i,2)) {
            flag = true;
            break;
        }
    }

    if(flag)
        System.out.println("It's power of 2");
    else
        System.out.println("It's not power of 2");

}

VBA Excel if Cell Is Not Equal to formula then

I want to check if a cell is this formula then execute a part 2 script

The code Simplified

Range("B1:C1").Select
If ActiveSheet.Name = "Sheet1" And ActiveCell <> "=formula1" Then Sheet1Macro

the complete code (long)

Range("B1:C1").Select
If ActiveSheet.Name = "Sheet1" And ActiveCell <> "=IF(OR(IFERROR(SEARCH(""DKB"",RC[-2]),0),IFERROR(SEARCH(""Cox"",RC[-2]),0),IFERROR(SEARCH(""2020"",RC[-2]),0)),RC[-1],"""")" Then Sheet1Macro

I'm just asking on how to check if the cell is a formula because I insert rows and that changes the formula from A2:F99 to A99:F189 for example

Adding Columns if they exist in the Dataframe: pandas

I have a Dataframe, where I want to add three columns and make it one, and I want it for the columns which exits in the data frame. so for example, I want to add following columns

List_ID=['ID1','ID2','ID3'] 
df # Df is the data frame

I am trying following for summing up the "if exists" columns, but not able to.

ID=sum[[col for col in list_ID if col in df.columns]] 

And explaining ID1, ID2 columns they can be like

df = pd.DataFrame({'Column 1':['A', '', 'C', ' '],'Column 2':[' ', 'F', ' ', '']})  
and my new column ID will look like 

In[34]: a=df['Column 1'] + (df['Column 2'])

In[35]: a

   Out[35]: 
    0    A 
    1    F
    2    C 
    3       

All suggestions are welcome

One-liner to save value of if statement?

Is there a smart way to write the following code in three or four lines?

a=l["artist"]
if a:
    b=a["projects"]
    if b:
        c=b["project"]
        if c:
            print c

So I thought for something like pseudocode:

a = l["artist"] if True:

Find same name of text file like in the first column and print text if condition is true

I have one text file and folder with another text files, delimited by tab. I would like to use condition if value smaller than 0 print specific text to another file with another condition.

For example:

  1. file:
112   0.6
116  -0.3
198  -0.7
205   0.4

  1. folder, where is 4 files with same name like in 1. file in first column.
112.tsv
116.tsv -here i would like to add last row with "This is the row what I want"
198.tsv -here i would like to add last row with "This is the row what I want"
205.tsv

I would like to use awk code:

awk '{if ($2<=0); print "This is the row what I want" "?? to file with same name like $1 ??"}' 1.file

Please could you help me?

Creating an If/Else Statement for Gamer Choices in Javascript

I'm pretty new to javascript but I'm trying to created a text based game. I've written a few scenarios and based on the player's decision I have different scenarios written out. As of now I have the code written and it works but I feel like I can be more efficient with an if else statement in my js.

Here's what I have so far in terms of player decisions.

<div class="sit_2">
                Should you go inside the cave, or do you search for                                                             food? 
</div>

                    <input type="button" class="choice_2a" value="Cave">

                    <input type="button" class="choice_2b" value="Food">
</html>
<javascript>
$(".choice_2a").click(function(){
    $(".sit_2").hide();
    $(".choice_2a").hide();
    $(".choice_2b").hide();
    $(".sit_3a").fadeIn(4000);
    $(".choice_3a").fadeIn(4000);
    $(".choice_3b").fadeIn(4000);

    });


$(".choice_2b").click(function(){
    $(".sit_2").hide();
    $(".choice_2a").hide();
    $(".choice_2b").hide();
    $(".sit_3b").fadeIn(4000);
    $(".choice_3a").fadeIn(4000);
    $(".choice_3b").fadeIn(4000);

    });

</javascript>

Not sure how clear it is, but picking choice_2a will fadeOut everything and fadeIn situation sit_3a along with choice_3a and choice_3b (which is the next situation and player choices). while choosing choice_2b will fadeOut everything and fadeIn in situation sit_3b along with choice_3a and choice_3b.

As I said this code does work and I've simplified it a bit here to avoid extraneous details but I feel like I can make it more efficient with an if/else statement but I'm not sure how to go about it. Any help would be greatly appreciated.

Also, should I make the choices into an array to make it easier?

Javascript Screen Width/Media Query If Statement That Returns an Alert Box if True

I am very new to Javascript and web development as a whole and I'm quite stuck with an If Statement here. The objective of this code is to read if the screen width (or media query) is below 450px, and if so display an alert box - I'm only needing this to work on one page at the moment.

The HTML:

either

<body onload="fullQuote()">

-or- (I'm not sure if the second actually works)

<script>window.onload = fullQuote()</script>

The JavaScript:

function fullQuote() {
var mediaQuery = window.matchMedia("(max-width: 450px)");
if (mediaQuery.matches) {
    alert("Test");
    }
}

I can't seem to find an answer as to whats wrong with this code anywhere, but I have tried different methods of finding out the screen size like

if (screen.width <= 450) {
if (document.documentElement.clientWidth < 450) {

and also a boolean within the if parentheses:

if (mediaQuery.matches == true) {

among some other ways around this now lost to memory which, of course, shared the same failed results.

Any help to where I am going wrong this matter would be greatly appreciated,

Thanks,

Will