Python 3. Input in if else statements - python-3.x

I am using string in if-else statement from input but not getting any result.

Two issues appear when I see that:
You use the is operator, which checks if two variables refer to the same object. x is y is only true if one was defined as equal to the other. Instead, you should use the == operator to check if two string are equal. Also, you type write, not writes, whereas your program is checking for writes. Entering the correct string and using the == operator instead of is should fix your problems.

Related

Sympy: solve assuming variable is positive

I would like to solve an equation assuming the variable is positive. I try to use assuming construction like
from sympy.assumptions import assuming, Q, ask
from sympy.abc import x
with assuming(Q.positive(x)):
sol = solve(Eq(x**2, 4))
print(sol)
But such a construction still gives two roots: [-2,2]. On the other hand if I initially declare variable x as x = symbols('x', positive=True) the function solve works fine. How actually assuming works?
To understand the whole idea of the assuming function, let's take a look at some points:
Queries:
I like to interpret Q (assumption keys this page is about Predicates) as "Query" or "Question".
The Q attribute provides a lot of predicates that you can use to test boolean values and expressions. So it works indeed like question and answer.
Q.predicate(boolean) is for generating a Predicate object that can be evaluated with
True, False and None.
ask(boolean) provides the answer for the query (may be a Predicate object or other boolean).
The assuming function itself:
When you use the with statement and the assuming function, you are creating a context for querying. This means you are passing arguments to assuming and using them inside the body of the with statement.
It is comparable to creating a virtual environment, different from the base environment. There you can use the values passed to assuming as they were axioms, literally assuming that they are True.
Another important point is that when you create a query instantiating an object of Q, it is intuitively intended to use the ask function to determine the value of the Predicate object or expression.
Also, you may find an interesting reading about the old and new assumptions of SymPy here (this page is about the old assumptions).
I hope this was helpful!

How to handle out-of-order assignments in an antlr4 visitor?

I have a simple antlr4 grammar that handles assignments of variables to text and OR operators (for recursive rules). My visitor works correctly when assignments are in order (example A), but fails when they are not (example B). How to make example B work as well? Perhaps I need some sort of two-pass approach where variables are created in the first-pass and resolved in the second? thanks
Example A (works)
$bar='happiness';
$foo='baz' $foo OR $bar;
$start=$foo;
$start // outcome is 'happiness' repeated one or more times
Example B (fails, $foo is undefined in line 1)
$start=$foo;
$foo='baz' $foo OR $bar;
$bar='happiness';
$start
Assuming your input entirely consists of a sequence of assignments followed by the return value (particularly I'm going to assume there aren't going to be any side-effecting operations between assignments), you can resolve the value of such a block of code by performing a depth first search starting with the result expression. To do that you would indeed use two phases like this:
Go over the assignments and create a map from variable names to expressions.
Evaluate the result expression recursively using the map to recursively evaluate variables appearing in the expression. If you encounter a cycle while doing this, that means there's infinite recursion in your input.

latex - pass argument with nested values

At the moment I have a command which simply takes an argument and does some mathematical operation on it:
\newcommand{\gobblenext}[1]{do some math with #1 and display it}
Now, in order to upgrade my home-made library, I also need to take into account different cases, when argument #1 is not just simply a single number. What I would need is a command which would deal with passing something more complex consisting of a description of the case and more values.
For instance:
\newcommand{\gobblenext}[1]{
if #1 has a given format R{num1}{num2} -> apply R function on num1 and num2
if #1 has a given format S{num1}{num2} -> apply S function on num1 and num2
else (if #1 is simple number) just do the math as in original function.
}
The truth is, that I have no clue what is the best approach. The reason why I cannot simply define three different commands for each case is because \gobblenext is used by other commands, so that I would have to rewrite a big portion of the library. Do you have any ideas?

Python3, range(a,b) function behaviour and empty lists

I am a bit confused about the behavior of the range() function in a specific use case.
When I was testing some code I wrote using nested FOR loops, in some cases, the statements in certain loops never seemed to execute at all. I eventually realized that I was in some cases feeding a range() call with an input like:
range(i,2) # where i is 2, giving range(2,2)
...which threw no error, but apparently never executed the for loop contents. After some reading on Python3's FOR implementation, I then added "else:" statements to my loop:
for i in range(a,b): # where a=b, i.e. range(2,2)
[skipped code]
else:
[other code]
...and the else-case code executed fine, as I guess all possible iterators for the given range values were (already) exhausted, and the for-else case was triggered as it's designed to be when that happens.
From what I can see in the documentation for range(), I found: "A range object will be empty if r[0] does not meet the value constraint." ( https://docs.python.org/3/library/stdtypes.html#range ). I'm not quite sure what the "value constraint" is in this case, but if I'm understanding right, "range(a,b)" will return an empty list if a >= b.
My question is, is my understanding correct about when range() returns []? Also, are there any other kinds of input cases where range(a,b) returns [], or other obscure edge case behaviors I should be aware of? Thank you.
as you can see in this documentation, when you use range(a,b) you're setting its start and stop parameters.
what you need to know is that stop parameter is always excluded just like in lists slicing.
another remark is that you can set the step, so if you set a negative step you can actually use a >= b like in this case:
range(10,4,-1)
Also please notice that all parameters need to be integers.
I recommend you visit the documentation provided above it's quite helpful.
range(n) generates an iterator to progress the integer numbers starting with 0 and ending with (n-1).
With reference to your FOR loop, it was not executed because the ending number (i.e. n - 1 = 2 - 1 = 1) is less than the starting number, 2. Since the step argument is omitted in your FOR loop, it defaults to 1. The step can be both negative and positive, but not zero.
Syntax:
range(begin, end[, step])
Examples:
Both examples below will produce empty list.
list(range(0))
list(range(2,2))

Excel WorksheetFunction.Or

Does WorksheetFunction.Or evaluate all arguments, or does it immediately return True once an argument evaluates to True?
The thing is, I am trying to check if a variable is numeric, and if it is, I want to check if it is less than 1. You can see it below:
If IsNumeric(lvl) Or lvl < 1 Then
Do sth...
End If
Described here, the Or operator in VBA evaluates all arguments and I get Type Mismatch Error when _lvl_ is not numeric. Does WorksheetFunction.Or behave the same?
Yes, WorksheetFunction.Or behaves the same.
The reason being, it is a regular function as far as VBA is concerned, so VBA will evaluate all arguments before passing them to the function. The function will not have a chance to perform lazy evaluation.
In order for lazy evaluation to kick in, the evaluation construct must be a part of the language syntax. VBA does not have it in its syntax, so it's not going to work when calling any function in any way.

Resources