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
Related
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
def space_check(board, position):
return board[position] == ' '
def full_board_check(board):
for i in range(1,10):
if space_check(board, i):
return False
return True
the last line is return True
why not else: return True
if the if statement returned false, won't the last return True overwrite it??
If it was
for i in range(1,10):
if space_check(board, i):
return False
else:
return True
then after the first iteration in the for loop the function would return. This would not lead the the expected behaviour. Currently, you check every space, and not just the first one
This function is supposed to return True if a number is prime and False if it's not. The problem is that is_prime(9) returns True.
def is_prime(x):
if x<2:
return False
elif x==2:
return True
elif x==3:
return True
else:
for i in range(2,x):
if x%i==0:
return False
break
else:
return True
Because if you write
for i in range(2,x):
if x%i==0:
return False
break
else:
return True
if x is 9 then 9%2 != 0, so it takes else path and returns True
odd_number % 2 is always = 1
You have to remove the last line and replace it with return True after the for has finished:
def is_prime(x):
if x<2:
return False
elif x==2:
return True
elif x==3: #<-- This one is useless, it will be already checked within the for
return True
else:
for i in range(2,x):
if x%i==0:
return False
break
return True
print is_prime(9)
print is_prime(11)
Your code is improperly indented. The else:return True line should be indented one level less, so that it runs after the for loop terminates. The current code returns True whenver any divisibilty test fails, not when all of the divisibility tests fail.
The function is buggy. The else: inside the loop is returning True the first time it encounters a number that isn't a divisor. That needs to be deleted, and the return True needs to be placed after the loop (outside of it).
Check indentation. The if and else in the for loop.
Understand the meaning of return, the function literally exits when you return.
Corrected code:
def is_prime(x):
if x<2:
return False
elif x==2:
return True
elif x==3:
return True
else:
for i in range(2,x):
if x%i==0:
print "Hello"
return False
break
else:
return True
print is_prime(9)
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
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