This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 6 years ago.
n = [3, 5, 7]
def double_list(x):
for i in range(0, len(x)):
x[i] = x[i] * 2
return x
print double_list(x)
The double_list call on the last line is giving me a SyntaxError.
In Python 3, the print syntax has been changed in order to bring more consistency with the rest of the Python syntax, which uses the brackets notation to call a function.
You must always do print(....) with the brackets, hence the SyntaxError.
print(double_list(x))
However, I do not see the rest of your code, so maybe you also have another iterable called x.
Otherwise you must also replace x by n, to avoid getting a NameError this time.
What is x in double_list(x), and why do you think it has that value?
Did you mean double_list(n)?
Related
This question already has answers here:
Recursive function in simple english [duplicate]
(6 answers)
Closed 11 months ago.
I was making some exercise to train myself and the exercise asked me to do a program that calculates fractals, very simple, i've done in about 1-2 minutes and it work, but looking at his solution it return x multiplicated by the function itself? how does this run? I know maybe it's a stupid question but i think it might be useful.
def fract(x):
if x == 0:
return 1
return x * fract(x - 1)
print(fract(int(input())))
Here's a walk through of whats going on.
First, you call fract(int(input())). The input method gets a response from the user and parses that to an int. and then calls fract on that int.
Say we enter 3. So our print statement evaluates to fract(3).
fract(3) returns 3 * fract(2)
fract(2) is called and that returns 2 * fract(1)
fract(1) is called and that returns 1
So putting it altogether and substituting function calls for what they return we get fract(3) returns 3 * (2 * (1)).
This question already has answers here:
Why doesn't .strip() remove whitespaces? [duplicate]
(3 answers)
Closed 1 year ago.
I want to learn what is the difference between two code lines.I couldn't find the difference.Whenever I try to run the second code,it doesn't affect string a.
Could someone tell me why the second code line doens't work?
a = "aaaaIstanbulaaaa".strip('a') #Affects the string
print(a)
>>>Istanbul
a = "aaaaIstanbulaaaa" #Doesn't affect the string
a.strip('a')
print(a)
>>>aaaaIstanbulaaaa
str.strip returns a value; it does not modify the str value invoking it. str values are immutable; you cannot modify an existing str value in any way.
The str.strip() isn't an inplace method, it doesn't change the current object you're calling with, it returns a new one modified
That is an example of inplace modification
x = [1, 2, 3]
x.append(4)
# x is [1, 2, 3, 4]
This question already has answers here:
"+=" causing SyntaxError in Python
(6 answers)
Closed 3 years ago.
I am trying to execute exec('global expression_result; expression_result = %s' % "a += 2") in python.
It is giving me SyntaxError. I have already declared the variables a and expression_result.
In ipython, I have also tried i = (a += 2) this is also giving the SyntaxError
How to evaluate these kinds of expressions and get the result?
First, you should not use exec or eval. There is almost never a need for either of these functions.
Second, an assignment (e.g., a+=2) is not an expression in Python (unlike C, C++, or Java). It does not have a value and cannot be printed or further assigned. You can split your code into two assignments, as advised by other commenters:
a += 2
i = a
You can't do it with the += sign.
But if you write it full it works.
i = a = a + 2
so drop i = a += 2 which is basically the shortcut for i = a = a + 2
As noted by the other answers, a += 2 is not an expression in Python, so it cannot be written where an expression is expected (e.g. the right-hand-side of another assignment).
If you do want to write an assignment as an expression, this is possible since Python 3.8 using the walrus operator, but you can only use it for simple assignments, not compound assignments:
expression_result = (a := a + 2)
I recreated your code and also, got a syntax error, any way you can use two lines?:
i = a
i += 2
Are you looking for this:
a+=2
i=a
This question already has answers here:
What is the __lt__ actually doing for lists [duplicate]
(2 answers)
Closed 4 years ago.
How come max([1,2,3], [1,1,4]) returns [1,2,3] not [1,1,4]?
I was asked this question in a class. I don't understand why it returns [1,2,3] and the logic behind it (even if it returns [1,1,4], I still don't understand what max() function does).
The function max will succeed in outputing a maximum as long as the provided arguments are comparable.
In this case, the first argument is greater than the second with regard to list-ordering.
>>> [1, 2, 3] > [1, 1, 4]
True
This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 5 years ago.
I'm trying to create a function that uses a while loop to count up from one to a number given by a user. The code executes as I intend it to but returns None at the end. How do I get rid of the None? Here's the code.
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
print (printFunction(int(input())))
You can use this code to prevent none, tough its just the last line changed
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
printFunction(int(input()))
In the last line you were using
print(printFunction(int(input()))) which was getting you None after printing the results.
Instead just use printFunction(int(input())). This will not print None. You can also use a message to ask user like printFunction(int(input("Enter a number"))). Since there is noting getting returned you no need to use print.