Python Function Codecademy [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to cube the number in this function, but Codecademy says it isn't returning the right result. Could anyone help?
def cube(number):
return number**number
def by_three(number):
if number % 3==0:
return cube(number)
else:
return False

Because it is not a cube. Cube is: number ** 3

Given your cube function, you are doing
n^n
for example, given n = 4, what you are really doing is 4*4*4*4
And it work only on 3 or it's multiple, given the line
if number % 3==0:

In case you are interested in one liner of this function check this out:
def by_three(number):
return number ** 3 if number % 3 == 0 else False
If you are interested in how this is done check ternary operator in python

Related

How should I write a recursive function that returns multiple values? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want the output to be a tuple including the sum the the product.
For example the list [1,2,3,4] should output (10,24). I can get the sum and product individually but cannot get both at the same time. Here's what I have so far:
def sum_product(aList):
if len(aList) == 1:
return aList[0]
result = sum_product(aList[1:])
return (result + aList[0], result*aList[0])
sum_product([1,2,3,4])
Help is greatly appreciated
Your mistake was that you return different types. In the end of recursion you return int, but in general return a tuple. I changed your code, so now it works.
def sum_product(aList):
if len(aList) == 1:
return aList[0], aList[0] #list[0] for sum and list[0] for product
result = sum_product(aList[1:])
return (result[0] + aList[0], result[1] * aList[0])

Python GCD program [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
def gcd(m,n):
fm = []
for i in range(1, m+1):
if (m%i) == 0 :
fm.append(i)
fn = []
for j in range(1, n+1):
if (n%j) == 0 :
fm.append(j)
cf = []
for f in fm:
if f in fn:
cf.append(f)
return(cf[-1])
print(gcd(56,23))
Although, there are other ways to find gcd using while loop and math.gcd commands, but my mentor wants this solution working as its mentioned in his programming book. How can I solve IndexError: list index out of range in the above program.
You don't append anything to cf.
In your 2nd for loop, change fm.append(j) to fn.append(j)

I am Wrote this python code but every time it's give me wrong answer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have wrote this Code for calculates the product of the first natural numbers, but it showing answer 1 every time. I don't where i did mistake?? Can you please help me find out my mistake in this code..
num = 10
i = 0
prod = 1
while i<=num:
i = i+1
prod*prod*i
print(prod)
The problem seems to be on the line prod*prod*i. The product needs to be accumulated and for this it should be exchanged for prod*=i.
The new snippet is:
num = 10
i = 0
prod = 1
while i<=num:
print(i)
i = i+1
prod*=i
print(prod)
Instead of prod*prod*i write prod=prod*i
Here we first take the input of the number of terms.Then we iterate the for loop and multiply the value of x with the initial value(=1).Then we assign the new value to p.
n=int(input('Terms: ')) #specifing the limit
p=1
for x in range(1,n+1):
p=p*x
print(p)

What's wrong in this python code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
def vector(sample_list):
for item in sample_list:
sums = 0
square = item**2
sums = square + sums
magnitude = sums**0.5
return magnitude
print(vector([2,3,-4]))
Why this code doesn't give the correct magnitude?
It gives the last value of the vector in function call.
change sums=0 position
def vector(sample_list):
sums = 0
for item in sample_list:
square = item**2
sums = square + sums
magnitude = sums**0.5
return magnitude

Why do I get None as a result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the following script:
import math
scores = [3.0,1.0,0.1]
sum = 0
i=0
j=0
for s in scores:
sum = sum + math.exp(scores[i])
i=i+1
def myFunction(x):
math.exp(x)/sum
for s2 in scores:
print(myFunction(scores[j]))
j=j+1
But, the output I get is:
None
None
None
Why is that? How can I retrieve the correct values?
Thanks.
You forgot to return.
def myFunction(x):
return math.exp(x)/sum
print(myFunction(scores[j]))
Here you try to print something. But myFunction doesn't return anything to print.
You can use,
def myFunction(x):
return math.exp(x)/sum
This will solve the problem.

Categories