Using Python 3.6.1. Syntax error in print statement [duplicate] - python

This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 5 years ago.
# Example for Algorithm Case Study
def naïve(a, b):
x = a
y = b
z = 0
while x > 0:
z = z + y
x = x - 1
return z
print naïve(4,5)
The output should be 20. Because of syntax error in print statement, I am not getting the answer.

print in Python 3 is a function, meaning you need to call it with parenthesis:
print(naïve(4,5))

Related

Python return function not giving value [duplicate]

This question already has an answer here:
Python script run through IDLE has no output
(1 answer)
Closed 2 years ago.
In python code, I've written:
x = 1
y = 2
def add_nums(first, second):
sum = first + second
return sum
And when I do:
add_nums(x, y)
It returns nothing what is wrong with the code?
The function returns its local sum variable. Perhaps you meant to print it
print(add_nums(x, y))

How Generator expressions work internally in python? [duplicate]

This question already has answers here:
What does the "yield" keyword do in Python?
(51 answers)
Why does the print function return None?
(1 answer)
Closed 2 years ago.
I have tried this following code:
result = (x for x in range(3))
for y in result:
print(y)
I am getting the following Output:
0
1
2
But when I am using this code :
result = (print(x) for x in range(3))
for y in result:
print(y)
I am getting the following output:
0
None
1
None
2
None
Can anyone explain, Why this None is coming in output in second code?
Because print(x) prints the value of x (which is the digits that get printed) and also returns None (which is the Nones that get printed)
print doesn't return a value, so you are seeing None, which is the return for print, and the output from print. This is a classic case of using comprehension syntax for side effects, which is not using the generator/comprehension syntax to contain an actual result. You can check this by running:
print(print('x'))
x
None

How to find a letter in a string in Python [duplicate]

This question already has answers here:
Python: Find a substring in a string and returning the index of the substring
(7 answers)
Closed 3 years ago.
Find out if word 'dog' is in a string.
I tried doing this code and i dont know where the error is .
y='dogaway'
for i in range(len(y)):
if y[i:i+2]=='dog':
x=x+1
print(x)
I expected output to be 1 but the actual ouput is 0.
You can use count.
y = 'dogaway'
print(y.count('dog')) # Outputs 1
or if you want to fix your code, you are just off by one in your splice:
y = 'dogaway'
x = 0
for i in range(len(y) - 3): # Make sure your range accounts for the look ahead
# In the future add a print to make sure it is doing what you expect:
# print(y[i:i + 2])
if y[i:i + 3] == 'dog': # Here you were off by 1
x = x + 1
print(x)
Even simpler:
if 'dog' in y:
...
You can use the in membership operator in Python.
'dog' in 'dogaway'
returns True

Can anyone please explain how does this function work? [duplicate]

This question already has answers here:
Understanding factorial recursion [duplicate]
(3 answers)
Closed 5 years ago.
I'm having trouble figuring out how does this exactly work. Can anyone please explain?
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)
x=int(raw_input())
print fact(x)
It gets the factorial of the value x. The value of x is whatever you enter since x = int(raw_input) gets the user input. For example if x=3, then fact(3) will be 3×2=6....

equals sign after a decrement operator in C++ code [duplicate]

This question already has answers here:
"num - 1" vs "num -= 1"
(9 answers)
Closed 8 years ago.
I have trying to translate code from C++ to python and on one line(in for loop), I have:
x -= (t = u/1.0+ MIN(c, EPS))
I want to know what the '=' sign after a decrement indicates? how can I translate this line in python
thank u
In c assigments are functions at there own, in Python assignments are expressions.
In python this means
t = u/1.0 + min(c, EPS)
x -= t # same as x = x - t

Categories

Resources