codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Python condition logic


By : , Category : python

"workspace" or "screen" or "desktop" or "switch" is an expression, which always evaluate to "workspace".

Python's object has truth value. 0, False, [] and '' are false, for example. the result of an or expression is the first expression that evaluates to true. "workspace" is "true" in this sense: it is not the empty string.

you probably meant:

"workspace" in command or "screen" in command or "desktop" in command or "switch" in command

which is a verbose way to say what @Ashwini Chaudhary has used any for.

ReLated :

Original expression:

( msg CONTAINS "A" ) or ( msg CONTAINS "B" ) or (msg CONTAINS "C") or (msg IS NULL)

Desired inverse:

NOT ( ( msg CONTAINS "A" ) or ( msg CONTAINS "B" ) or (msg CONTAINS "C") or (msg IS NULL) )

DeMorgan's Theorem applied:

NOT( msg CONTAINS "A" ) AND NOT( msg CONTAINS "B" ) AND NOT(msg CONTAINS "C") AND NOT(msg IS NULL)

Using available primitives:

( msg NOT CONTAINS "A" ) AND ( msg NOT CONTAINS "B" ) AND (msg NOT CONTAINS "C") AND (msg IS NOT NULL)

Optionally, using the commutative law of conjunction, move the NOT NULL test first:

(msg IS NOT NULL) AND (msg NOT CONTAINS "A") AND (msg NOT CONTAINS "B") AND (msg NOT CONTAINS "C")

(Moving the NOT NULL test first may help if NOT CONTAINS fails when given a null first argument or if msg is NULL often and the system avoids performing the rest of the NOT CONTAINS checks.)

you are doing too many "if", do you really need that many? that makes your code hard to debug.

try something like

@products = {}
["Stationery","Food","Toiletries","Household","Clothing","Accessories"].each do |dep|
  @products[dep] = Product.where(department: dep).first
end

and replace all your view with:

<%- ["Stationery","Food","Toiletries","Household","Clothing","Accessories"].each do |dep| -%>
  <div class="column_entry">
    <%- if product = @products[dep] -%>
      <%= link_to image_tag(product.product_image.url(:normal_page_size)), products_content_url(product), :controller=>'products' -%>
    <%- end -%>
  </div>
<%- end -%>

EDIT: changed the code a little, you could even have a constant for that deparments array to be dryer

for name if i don't give any value( i.e simply pressing enter)

That does not leave name with the value of a string with a single space - i.e. the one that your if is looking for. Print the values that you get back to see what you are getting, then modify the condition accordingly:

System.out.println("number=|"+ number + "|, name=|" + name + "|");

The above adds delimiters to the strings so that you could easily determine the length of their content.

You have to check few conditions for each time, need not to consider the sequence. Possible cases of win for any player would be: 0,1,2

0,3,6

0,4,8

1,4,7

2,4,6

2,5,8

3,4,5

AND 6,7,8

I am assuming that numbering is row wise and started from 0.

Now you have to tell computer based on the input of user.

For example:

User has selected 2, so you have posStatus[2] = 1 Now the only cells you have to check are 0,1,4,5 and 6 as these are the only possible cells for win.

To handle the beginning of game situation, you have two options:

  1. Let the user start always.
  2. Start anyone from 0,2,6 and 8 as these are part of the maximum solutions.

Another Approach:

There are 3 possibilities of success:

A. Row wise : Add or substitute number by 1 and check status ,

B. Column wise : Add or substitute number by 3 and check status ,

C. Diagonally : Add or substitute number by 2 AND 4 then check status

For example if user has selected i cell. Your check should be:

A. See the status of (i-2),(i-1),(i+1) AND (i+2) - Obviously this should be in range of 0-8

B. See the status of (i-6),(i-3),(i+3) AND (i+6)

C. See the status of (i-8),(i-4),(i-2),(i+2),(i+4) AND (i+8)

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .