'Very simple function gives no output ( Python 3.7) [closed]
First-Timer here :)
The very simple function geome(x) gives no output, eventhough the exact same code works fine outside of the function (see "z = list ...").
What am I doing wrong?
Would very much appreciate some help or hintsThank you.
from operator import truediv
x = [1, 2, 4, 8, 16]
z = list(map(truediv, x[1:], x[:-1])) # This Works perfectly fine! >>> Geometric
if all(num == z[0] for num in z):
print('Geometric')
def geome(x):
z = list(map(truediv, x[1:], x[:-1]))
if all(num == x[0] for num in z):
print('Geometric')
geome(x) # Doesn't work, even though it is the same code as above, only inside of a function. >>>
Solution 1:[1]
Your geome implements something different to what you have outside. In the if clause you changed z[0] to x[0], so geome should be like this instead:
def geome(x):
z = list(map(truediv, x[1:], x[:-1]))
if all(num == z[0] for num in z):
print('Geometric')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | molinav |
