conditional-statements

Conditional with statement in Python

穿精又带淫゛_ 提交于 2019-11-27 00:41:30
问题 Is there a way to begin a block of code with a with statement, but conditionally? Something like: if needs_with(): with get_stuff() as gs: # do nearly the same large block of stuff, # involving gs or not, depending on needs_with() To clarify, one scenario would have a block encased in the with statement, while another possibility would be the same block, but not encased (i.e., as if it wasn't indented) Initial experiments of course give indentation errors.. 回答1: If you want to avoid

Can you use if/else conditions in CSS?

放肆的年华 提交于 2019-11-27 00:01:10
I would like to use conditions in my CSS. The idea is that I have a variable that I replace when the site is run to generate the right style-sheet. I want it so that according to this variable the style-sheet changes! It looks like: [if {var} eq 2 ] background-position : 150px 8px; [else] background-position : 4px 8px; Can this be done? How do you do this? Boldewyn Not in the traditional sense, but you can use classes for this, if you have access to the HTML. Consider this: <p class="normal">Text</p> <p class="active">Text</p> and in your CSS file: p.normal { background-position : 150px 8px; }

Alternative to Switch Case in Java

我与影子孤独终老i 提交于 2019-11-26 20:58:52
问题 Is there any alternative way to implement a switch case in Java other than if else which is not looking good. A set of values will be there in combination, according to the selection corresponding method has to be executed. 回答1: Presumably you're struggling with the requirement of case's being constant. Typically this is a code-smell, but there are things you can do. You might want to raise and link to another question that details why you're trying to switch. Map<String,Object> map = new

Difference between JA and JG in assembly

北战南征 提交于 2019-11-26 20:42:59
Can you please tell me the difference between JUMP IF ABOVE AND JUMP IF GREATER in Assembly language? when do i use each of them? do they give me different results? harold As Intel's manual explains , JG interprets the flags as though the comparison was signed, and JA interprets the flags as though the comparison was unsigned (of course if the operation that set the flags was not a comparison or subtraction, that may not make sense). So yes, they're different. To be precise, ja jumps if CF = 0 and ZF = 0 (unsigned Above: no carry and not equal) jg jumps if SF = OF and ZF = 0 (signed Greater,

Angular 2 Pipe under condition

别来无恙 提交于 2019-11-26 20:05:03
问题 Is it possible in Angular 2 to apply a pipe under condition? I would like to do something like: {{ variable.text | (variable.value ? SomePipe : OtherPipe) }} If not, what is the preferred way to achieve this effect? 回答1: You need to change the syntax a bit: {{variable.value ? (variable.text | SomePipe) : (variable.text | pipe2)}} Plunker example 回答2: Since such syntax isn't supported, I think that the only way to do that is to implement another pipe to handle the condition: @Pipe({ name:

How can I check if a string only contains letters in Python?

家住魔仙堡 提交于 2019-11-26 18:48:31
I'm trying to check if a string only contains letters, not digits or symbols. For example: >>> only_letters("hello") True >>> only_letters("he7lo") False Simple: if string.isalpha(): print("It's all letters") str.isalpha() is only true if all characters in the string are letters: Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Demo: >>> 'hello'.isalpha() True >>> '42hello'.isalpha() False >>> 'hel lo'.isalpha() False The str.isalpha() function works. ie. if my_string.isalpha(): print('it is letters') For people finding this

C++ multiple strings inside an if statement

 ̄綄美尐妖づ 提交于 2019-11-26 18:37:20
问题 I'm having an issue trying to check against multiple possibilities in an if statement. The user inputs a string, and then I check that string against multiple possibilities. if (theString == "Seven" || "seven" || "7") { theInt = 7; cout << "You chose: " << theInt << endl; } else if (theString == "Six" || "six" || "6") { theInt = 6; cout << "You chose: " << theInt << endl; } So there's just a quick example of what I'm trying to accomplish. In my program, these if statements are in a function,

Compound condition in C: if (0.0 < a < 1.0)

百般思念 提交于 2019-11-26 18:34:51
问题 I recently noticed that the following expression compiles in my compiler (Clang): float a; if (0.0 < a < 1.0) { ... } Does this do what I expect? I would expect that it evaluates identically to this condition: if (0.0 < a && a < 1.0) { ... } If it is so, since when and with which other compilers it is possible to write conditions in this short form? 回答1: Because of left-to-right associativity of < operator the expression condition (0.0 < a < 1.0) means ((0.0 < a) < 1.0) == 1 < 1.0 or 0 < 1.0

Resetting the State of a Stream

给你一囗甜甜゛ 提交于 2019-11-26 17:04:57
问题 I have a question which is slightly similar to this question on stackoverflow std::cin.clear() fails to restore input stream in a good state, but the answer provided there does not work for me. The question is: how can I reset the state of a stream to 'good' again? Here is my code how I try it, but the state is never set to good again. I used both of the lines ignore separately. int _tmain(int argc, _TCHAR* argv[]) { int result; while ( std::cin.good() ) { std::cout << "Choose a number: ";

SELECT query with CASE condition and SUM()

最后都变了- 提交于 2019-11-26 16:16:21
问题 I'm currently using these sql statements. My table has the field CPaymentType which contains "Cash" or "Check". I can sum up the amount of payments by executing 2 SQL statements as shown below. In this case, the user won't even notice the speed difference when executing 2 sql statements or just 1, however, I don't like my way, I just want 1 sql statement. How do I reconstruct these into 1 statement with CASE conditions? I can't figure it out since examples online result in either 1 or 0 or