Codecademy using functions with python - python

I am a new programmer learning python with Codeacademy and I am writing a program that takes an argument number and returns the cube of that number and I keep getting the error:
by_three(1) returned 1 instead of False
I am trying to print the cube, not False.
def cube(number):
return number ** 3
def by_three(number):
if number % 3:
return number
else:
return False

I have went onto codecademy's python tutorial and found the section with this exercise.
Here is the working code:
def cube(number):
return number**3
def by_three(number):
if number % 3 == 0:
return cube(number)
else:
return False
Essentially, the exercise simply wanted you to call the first function if the number was divisible by 3.

The return under if number % 3 == 0: is not providing the correct calculation. Try the following:
def cube(number):
return number**3
def by_three(number):
if number % 3 == 0:
return cube(number)/3
else:
return False

Related

Why is my Python prime number checker not working?

Why is my code below giving me wrong results? When I give it numbers like 2 and 3, it says they're not prime numbers. Only some numbers work but for the most part it gives wrong answers.
def prompt_input(input_msg, error_msg):
while True:
userinput = input(input_msg)
try:
integer = int(userinput)
if integer > 1:
return integer
print(error_msg)
except ValueError:
print(error_msg)
def check_prime(number):
for i in range(2, number):
if number % i == 0:
return False
return True
primenum = prompt_input(
"Give an integer that's bigger than 1: ",
"You had one job"
)
if check_prime(primenum):
print("This is a prime.")
else:
print("This is not a prime.")
First issue: Your for loop returns after one iteration, so the correct return logic would be:
def check_prime(number):
for i in range(2, number):
if number % i == 0:
return False
return True
However you are implementing this incorrectly as well, the loop needs to iterate up to the sqrt of the number.
def check_prime(number):
for i in range(2, int(sqrt(number))+1):
if number % i == 0:
return False
return True
Hope this helps!
Need to unindent the return True.
def check_prime(number):
for i in range(2, number):
if number % i == 0:
return False
return True

Python: Why donnot I get a result in the terminal

Python: Why don't I get a result in the terminal
def is_even(number):
if number % 2 == 0:
return True
return False
is_even(10)
You need to print the output of the function. You also need to create the function.
def is_even(num):
if num % 2 == 0:
return True
else:
return False
print(is_even(10)) #True
print(is_even(7)) #False

Why is my function being ignored?

In the console, my program prints the first question, and once the input is entered, prints the second one and terminates. It appears to skip the function. Obviously I've done something(s) wrong, any help would be appreciated. That while-loop still feels wrong.
def Prime(n):
i = n - 1
while i > 0:
if n % i == 0:
return False
print("This number is not prime.")
else:
i = i - 1
return True
print("This number is prime.")
def Main():
n = int(input("What is the number you'd like to check?"))
Prime(n)
answer2 = input("Thank you for using the prime program.")
Main()
Your function returns before printing output, so nothing ever gets to the console. Consider printing before returning:
def Prime(n):
i = n - 1
while i > 0:
if n % i == 0:
print("This number is not prime.") # Here
return False
else:
i = i - 1
print("This number is prime.") # And here
return True

returning a cube function

I have two functions cube which returns a number cubed and by_three which, if cube is divisible by 3, I need to return cube, else return false. here is what I have so far(below). I keep getting the "Oops, try again. by_three(3) returned True instead of 27 " error, some please help if you know what im doing wrong, or perhaps idiotic!.
def cube(number):
return number**3
def by_three(number):
return number%3==0
if bythree(number):
return cube(number)
else:
return false
Your indentation is all over the place but this will do what you want:
def cube(number):
return number**3
def by_three(number):
return number%3==0
def main(number):
return cube(number) if by_three(number) else False
if by_three(number) is True cube(number) will be called and returned else just False will be returned.
Your code is either unreachable after the return or you have it outside the function where a return will not work. There is also no false in python it is upper case F `
You need a third method.
def cube(number):
return number**3
def by_three(number):
return number%3==0
def whattodo(number):
if by_three(number):
return str(cube(number)) #We must return one type, so we return string for both false and number
else:
return "false"
try:
print int(whattodo(input("Enter a number")) #We are trying to convert the string into an integer
except ValueError: #What if it is not a number? (In this case, it will be a string whose value is "false"
print "Your number is not divisible by three. Screw you"
Try this:
def cube(number):
return number**3
def by_three(number):
if number % 3 == 0:
return cube(number)
else:
return False

Python issues with return statement

Hello I'm very new to python and was wondering if you could help me with something.
I've been playing around with this code and can't seem to get it to work.
import math
def main():
if isPrime(2,7):
print("Yes")
else:
print("No")
def isPrime(i,n):
if ((n % i == 0) and (i <= math.sqrt(n))):
return False
if (i >= math.sqrt(n)):
print ("is Prime: ",n)
return True
else:
isPrime(i+1,n)
main()
Now the output for the isPrime method is as follows:
is Prime: 7
No
I'm sure the function should return true then it should print "Yes".
Am I missing something?
You are discarding the return value for the recursive call:
def isPrime(i,n):
if ((n % i == 0) and (i <= math.sqrt(n))):
return False
if (i >= math.sqrt(n)):
print ("is Prime: ",n)
return True
else:
# No return here
isPrime(i+1,n)
You want to propagate the value of the recursive call too, include a return statement:
else:
return isPrime(i+1,n)
Now your code prints:
>>> isPrime(2,7)
is Prime: 7
True

Categories

Resources