I just started up the new version of python, and realized that a lot has changed. Anyways, eclipse comes up with a red "X" mark beside the line numbers saying "Expected::". Could someone please explain what this means, and how I can get rid of it?
This is the code I'm trying to make work with Eclipse and the new Python version:
print "Please insert a valid operator that you want to calculate with."
print "Valid operators are +, -, : and *"
operator = str(raw_input("What's your operator? "))
numb1 = int(raw_input("Please insert the first number:"))
numb2 = int(raw_input("Please insert the second number:"))
if operator == "+":
print numb1 + numb2
elif operator == "*":
print numb1 + numb2
elif operator == "-":
print numb1 - numb2
elif operator == "/":
print numb1 / numb2
On Python3, print is a function, not a statement, so it should be written (for example)
print("Please insert a valid operator that you want to calculate with.")
Also raw_input has been renamed to input so it should be (for example):
numb1 = int(input("Please insert the first number:"))
I ran this program and I saw no problems with it when I ran it on Pydev on Eclipse, even though I ran it on 2.7. perhaps it has something to do with your indentation.
operator = str(raw_input("What's your operator? "))
numb1 = int(raw_input("Please insert the first number:"))
numb2 = int(raw_input("Please insert the second number:"))
if operator == "+":
print numb1 + numb2
elif operator == "*":
print numb1 + numb2
elif operator == "-":
print numb1 - numb2
elif operator == "/":
print numb1 / numb2
I copy pasted your example as well and to fix it I just had to fix the indentation as StackXchangeT did.
However I got Expected:: error when there was missing finishing : at the end of declaration e.g.:
class MyInvalidClass
which is supposed to be:
class MyInvalidClass:
Perhaps you get such error in similar situations where : is needed (just a guess).
Related
I am trying Scala for the very first time, not sure how to filter based on not contains.
I have a below query;
.filter(_.get[Option[String]]("status") map(_ split "," contains "Pending") getOrElse(false))
But I want to do something like below;
.filter(_.get[Option[String]]("status") map(_ split "," does not contain "Pending") getOrElse(false))
Can someone please help?
You can use exists and forall to simplify this. These functions return true if a condition is true for any or all elements of a collection.
So the pattern
Option[String].map(???).getOrElse(false)
can be
Option[String].exists(???)
And the condition
!(a split "," contains "Pending")
can be
a.split(",").forall(_ != "Pending")
Applying both of these to the original code gives
.filter(_.get[Option[String]]("status").exists(_.split(",").forall(_ != "Pending")))
But I would recommend a local function to clarify this code:
def notPending(s: String) = s.split(",").forall(_ != "Pending")
.filter(_.get[Option[String]]("status").exists(notPending))
This reads as "take all values where the status option exists and the status is not pending"
Figured out the solution;
.filter(_.get[Option[String]]("status") map(a => !(a split "," contains "Pending")) getOrElse(false))
Thanks.
I am using a number inside a calculation and have to turn it into a string. When I do this, I lose the comma formatting. Using another post:
Changing Number Format in a String
However the formula, I am using:
STR(ROUND(LOOKUP(sum([Actual]),0),0))
Does not seem to be working. The full calculation is here:
If attr([Kpi Nm]) = "Policy Retention Better-than-State Average"
Then str(round(sum([Actual]) * 100, 2)) + "%"
ElseIf attr([Kpi Nm]) = "Policy Retention Improvement (or > 90%)**"
Then str(round(sum([Actual]) * 100, 2)) + "%"
Elseif attr([Kpi Nm]) = "Premium Growth"
Then str(round(sum([Actual]) *100, 2)) + "%"
Elseif attr([Kpi Nm]) = "PIF Growth"
Then str(round(sum([Actual]), 2))
Elseif attr([Kpi Nm]) = "Product Density"
Then "NA"
else
STR(ROUND(LOOKUP(sum([Actual]),0),0))
End
I need to keep the commas on the Actual for the else statement. Any help provided would be much appreciated. The Lookup is supposedly a trick to make it work but does not work in my case.
Thanks,
This should work to keep the commas on the Actual else statement. I had a similar issue and combined a few found answers and this worked for me.
REGEXP_REPLACE(STR(SUM([Actual])),"(\d)(?=(\d{3})+$)","$0,")
I found this regular expression on another site and haven't personally tried it. Not sure if Tableau's regex implementation supports this particular type of expression though because it needs to look ahead. Use it with the regular expression replace function.
(\d)(?=(\d{3})+$)
I am creating a simple chat programme in QBasic that will answer questions based on some specific key words present in the user input.therefore I need a way to search for a sub string (I.e. A specific word)within a string.
So, please help me.
To find out if a string contains a certain (sub-)string, you can do this:
text$ = "nonsense !"
IF INSTR( text$, "sense" ) >= 1 THEN
PRINT "This text makes sense !"
END IF
And no, I was not able to test this, as a no longer have QBasic on my PC ;-)
According to the link from the comment above >= 1 is ok
I think INSTR is usually used as follows:
sent$ = "This is a sentence"
PRINT INSTR(1, sent$, "is")
PRINT INSTR(4, sent$, "is")
PRINT INSTR(1, sent$, "word")
the first PRINT command will print a '3' since the first location of "is" within the sentence is at position 3. (The 'is' in 'This')
the second PRINT command starts searching at position 4 (the 's' in 'This'), and so finds the "is" at position 6. So it will print '6'.
the third PRINT command will print a '0' since there is no instance of "word" in the sentence.
Counts the occurrences of a substring within a string.
T$ = "text to be searched and to be displayed"
S$ = "to"
l = 1
DO
x = INSTR(l, T$, S$)
IF x THEN
n = n + 1
l = x + LEN(S$)
ELSE
EXIT DO
END IF
LOOP
PRINT "text '"; S$; "' matches"; n; "times."
I'm struggling with a while loop problem. Once the calculation is done, I want the user to be asked whether they want to close the calculator or clear the screen and retry.
How can I use the while loop to do that, if at all?
print ("Calculator Program")
clear="c"
while clear=="c":
one=input("Enter your first number:")
two=input("Enter your second number:")
operator=input("""Choose an operator:
1) Add
2) Subtract
3) Multiply
4) Divide
""")
if operator == '1':
ans=float(one)+float(two)
print (ans)
elif operator == '2':
ans=float(one)-float(two)
print (ans)
elif operator == '3':
ans=float(one)*float(two)
print (ans)
elif operator == '4':
ans=float(one)/float(two)
print(ans)
For the last line in your loop add:
clear = input("[c]lear or [e]xit")
If the user types anything besides "c" the loop will break and the program will finish execution
My list (#degree) is built from a SQL command. The NVL command in the SQL isn't working, neither are tests such as:
if (#degree[$i] == "")
if (#degree[$i] == " ")
if (#degree[$i] == '')
if (#degree[$i] == -1)
if (#degree[$i] == 0)
if (#degree[$i] == ())
if (#degree[$i] == undef)
$i is a counter variable in a for loop. Basically it goes through and grabs unique degrees from a table and ends up creating ("AFA", "AS", "AAS", "", "BS"). The list is not always this long, and the empty element is not always in that position 3.
Can anyone help?
I want to either test during the for loop, or after the loop completes for where this empty element is and then replace it with the word, "OTHER".
Thanks for anything
-Ken
First of all, the ith element in an array is $degree[$i], not #degree[$i]. Second, "==" is for numerical comparisons - use "eq" for lexical comparisons. Third of all, try if (defined($degree[$i]))
Everything that Paul said. And, if you need an example:
my #degree = ('AFA', 'AS', 'AAS', '', 'BS');
$_ ||= 'OTHER' for #degree;
print join ' ' => #degree; # prints 'AFA AS AAS OTHER BS'
If its actually a null in the database, try COALESCE
SELECT COALESCE(column, 'no value') AS column FROM whatever ...
That's the SQL-standard way to do it.