a = raw_input("What do you want to add to the list?"
" If you would like to remove something from the list type 'remove' : ")
if str(a) not == "remove".lower():
print('"'), str(a), ('"' " has been added to list")
list1.append(a)
print(list1)
When i run this (there is more code but this is the relevant bit) it says "expected ':'" just before the not in the "if str(a) not == "remove".lower():" line. keep in mind that this is on python 2.7
To test for inequality in Python this is valid:
A != B
and so is this:
not (A == B)
but this is not:
A not == B # syntax error
(You could be forgiven for thinking that's how it works if you've been using the in operator, because not (A in B) and A not in B are equivalent.)
Once you have fixed that, know that this:
print('"'), str(a), ('"' " has been added to list")
is cursed, and only accidentally does what you want in Python 2 (it won't do what you evidently assume it does in any currently-supported Python version). When you want to print() something, the specification of what is to be printed ends at that first ). The following works in both Python 2 and Python 3:
print( '"{}" has been added to list'.format(a) )
Then there's the fact that you are applying .lower() to the wrong side of a comparison (to something that is already always lower-case).
Related
I created a simple code in Scala that checks whether an input is correctly formatted as HH:mm. I expect the code to result in an Array of valid strings. However, what I'm getting as a result is of type Any = Array(), which is problematic as when I try to print that result I get something like that:
[Ljava.lang.Object;#32a59591.
I guess it's a simple problem but being a Scala newbie I didn't manage to solve it even after a good few hours of googling and trial & error.
val scheduleHours = if (inputScheduleHours == "") {
dbutils.notebook.exit(s"ERROR: Missing param value for schedule hours.")
}
else {
val timePattern = """^((?:[0-30]?[0-9]|2[0-3]):[0-5][0-9])$""".r
val inputScheduleHoursParsed = inputScheduleHours.split(";").map(_.trim)
for (e <- inputScheduleHoursParsed) yield e match {
case timePattern(e) => e.toString
case _ => dbutils.notebook.exit(s"ERROR: Wrong param value for schedule hours: '${inputScheduleHours}'")
}
}
The problem is that some branches return the result you want and others return dbutils.notebook.exit which (I think) returns Unit. Scala must pick a type for the result that is compatible with both Unit and Array[String], and Any is the only one that fits.
One solution is to add a compatible value after the calls to dbutils.notebook.exit, e.g.
val scheduleHours = if (inputScheduleHours == "") {
dbutils.notebook.exit(s"ERROR: Missing param value for schedule hours.")
Array.empty[String]
}
Then all the branches return Array[String] so that will be the type of the result.
I've made some basic progress in python before, nothing more than command land algebra calculators to do math homework, using user-defined functions, inputs, and basic stuff. I've since taken the Python 2 course that codeacademy has, and I'm finding no equivalent of using % and %s for PY3.
I've narrowed it down to having some relation to format() , but that's as far as I could find on Google.
As a beginner, I'd really appreciate a watered down explanation to how to translate this into Python 3:
str1 = "Bob,"
str2 = "Marcey."
print "Hello %s hello %s" % (str1, str2)
EDIT: Also, I'm aware that print("Hello " + str1 + "hello " + str2) would work.
str.__mod__() continues to work in 3.x, but the new way of performing string formatting using str.format() is described in PEP 3101, and has subsequently been backported to recent versions of 2.x.
print("Hello %s hello %s" % (str1, str2))
print("Hello {} hello {}".format(str1, str2))
This should work as intended:
str1 = "Bob,"
str2 = "Marcey."
print("Hello {0} hello {1}".format(str1, str2))
While the use of % to format strings in Python 3 is still functional, it is recommended to use the new string.format(). It is more powerful and % will be removed from the language at some point.
Go on the Python website to see changes from Python 2.7 to Python 3 and the documentation contains everything you need.
:)
The % operator is not related to print; rather, it is a string operator. Consider this valid Python 2.x code:
x = "%s %s" % (a, b)
print x
Nearly identical code works in Python 3:
x = "%s %s" % (a, b)
print(x)
Your attempt would be correctly written as
print("%s %s" % (a, b))
The % operator is analogous to the C function sprintf, not printf.
Use f-strings in Python 3
prefix the string with f or F for variables and expressions inserted between curly braces inside the string to be evaluated inline:
str1 = "John Cleese"
str2 = "Michael Palin"
age1 = 73
age2 = 78
print(f"Hello {str1}, hello {str2}, your ages add to {age1 + age2}.")
Note the Python3 brackets in print(). Apparently, string interpolation is faster than the .format() syntax of Python 2.
The method you are using is still available in Python 3 (str.mod()).
In addition to this you can make use of string formatting in Python.
e.g:
print("This is {}".format("sparta")) #gives output
"This is sparta"
or
a = "Sparta"
b = "King"
print("This is {} and I am its {}".format(a,b)) #gives output
"This is Sparta and I am its King"
I would like to ask about the if else expression in ireport jasper report. May I know is it possible to have multiple or more parameter in the if else statement?
(($P{endDate}.isEmpty()==true || $P{endDate}.equals(""))? "" :
" createDate>='" + $P{startDate} +"'" && " createDate<='" + $P{endDate} +"'")
Based on the code above, there are not allowed me to use "&&". It prompt out syntax error.
Besides that, May I know any solution to solve it? Thank you very much.
I'm assuming it's a query expression your trying to write. You probably would have to do something as follows:
Create a parameter for your dataset. This parameter should not be "prompted" and lets call it DATE_LIMIT_EXPRESSION. You should then set its default value as your expression. For example (if I a get what you meant), this could be your default expression:
"1" +
(($P{startDate}.isEmpty() == false) ? (" AND createDate >= " + $P{startDate}) : "") +
(($P{stopDate}.isEmpty() == false) ? (" AND stopDate <= " + $P{stopDate}) : "")
Now, your dataset query should be something like:
select
...
where $P!{DATE_LIMIT_EXPRESSION}
Just notice the "$P!" syntax. You can find more information about this in the Jasper Reports' documentation.
so here is my code
a="yes"
b="no"
c=a[0].upper() + a[1:]
d=b[0].upper() + b[1:]
e=a.upper()
f=b.upper()
def shut_down(s):
if s == a or c or e:
return "Shutting down..."
if s == b or d or f:
return "Shutdown aborted"
else:
return"yeah"
so when i call up the function it doesn't execute properly(it doesn't run through all the if statement) i am new to python and don't know why is this but when i redo the work like the following it works as intended
a="yes"
b="no"
c=a[0].upper() + a[1:]
d=b[0].upper() + b[1:]
e=a.upper()
f=b.upper()
def shut_down(s):
if s == a:
return "Shutting down..."
if s== e:
return "Shutting down..."
if s ==c:
return "Shutting down..."
if s == b:
return "Shutdown aborted!"
if s == d:
return "Shutdown aborted!"
if s == f:
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
can anyone tell me why is this happening
The issue is with the expression s == a or c or e (and its variations) which doesn't do what you intend.
It is evaluated like this: (s == a) or bool(c) or bool(e). Since c and e are always "truthy" values (being non-empty strings), the or expression ends up always being true.
There are a few alternative solutions:
You could distribute out the equality test in your current version of the expression, resulting in s == a or s == c or s == e, but that's a lot of repeated text.
Another alternative is to use the in operator which tests if a value is in a container: s in (a, c, e). That's less typing, but it still does three tests.
A better solution would allow you to avoid the multiple tests completely. Instead, you can make a single test that is completely case-insensitive: s.lower() == a.
With the third solution, there's no need to calculate the variables c, d, e, or f (and you could even put the "yes" and "no" literals directly in the tests, allowing a and b to go away too!). It will match a few more input texts (e.g. "yEs"), but that's probably a feature, rather than a bug.
This is an issue commonly experienced by new programmers; the computer does not behave how one would expect it to, but rather how it is told to.
The equality comparison is not distributed across all or terms, but rather is only performed on only the first. You must provide it in all terms yourself:
if s == a or s == c or s == e:
Python also has an alternate syntax for this (although operation is not quite the same), based on containment checking:
if s in (a, c, e):
In this case we have only two operands for in, and distribution across the tuple elements is not required.
I have this if statement and when I run it it reeturns an error : " ')' expected "
if (a=1 and b=4 and c=width/2) or (a=2 and b=1 and c=width/2) then
...
end
Am I doing something wrong here? or is it something wrong with lua?
Try to replace your current code with this:
if (a==1 and b==4 and c==width/2) or (a==2 and b==1 and c==width/2) then
...
end
= means assignment, whereas == checks for equality and it looks like you want to check for equality.