I'm looking to write a function definition named has_evens that takes in sequence of numbers and returns True if there are any even numbers in the sequence and returns False otherwise.
My code is correct, except when it receives something empty, like "([])". I need to account for that. Here's my code:
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
elif num % 2 != 0:
return False
if ([]):
return False
The final part is a desperate attempt to account for blank lists. More formally, it needs to pass this assertion:
assert has_evens([]) == False
You should only return True when an even is found:
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
return False
Python has an any function to make this simpler:
def has_evens(s):
return any(num % 2 == 0 for num in s)
I answered my own question. I just needed to un-indent my lines
def has_evens(s):
for num in s:
if num % 2 == 0:
return True
else:
return False
Ok, so here is my code:
def is_prime(n):
n = abs(int(n))
if n < 2:
return False
elif n == 2:
return True
elif n%2 == 0:
return False
else:
prime(n)
def prime(n):
for x in range(3, int(n**0.5)+1,2):
if n%x == 0:
return False
else:
return True
print is_prime(6577)
But whenever I run this in my shell it returns 'None', I don't understand why. Any help would be appreciated.
Your final else in is_prime returns nothing. You can even remove the else altogether, but that's just personal preference
def is_prime(n):
# You don't really need to take abs value cause you already do a check for < 2 which includes negative numbers
n = abs(int(n))
if n < 2:
return False
elif n==2:
return True
elif n%2 == 0:
return False
return prime(n)
I'd like to understand something in Python - IF condition.
Using this example code:
num = 1
if (num == 1):
print ("op 1")
elif (num < 2):
print ("op 2")
elif (num <= 1):
print ("op 3")
elif (num != 1):
print ("op 4")
else:
print ("Fail")
print ("End")
I'll get the first condition and then go to the end.
How can I do it to check, for example, the first and if True go check the next condition too? And check all of them?
Trying to use CONTINUE not worked; I used a bunch of IFs instead of IF->Elif, but is this the right way to do?
I used a bunch of IFs instead of IF->Elif, but is this the right way to do?
This is exactly the right way to do it. Any time an if statement is true, the remaining elif statements are skipped. The only way to ensure that all are checked is with if statements.
You need a separate if statement for each condition. However, you need to keep track of when a condition applies to handle the case where none are true in order to print "Fail".
num = 1
none = True
if (num == 1):
print ("op 1")
none = False
if (num < 2):
print ("op 2")
none = False
if (num <= 1):
print ("op 3")
none = False
if (num != 1):
print ("op 4")
none = False
if none:
print ("Fail")
print ("End")
This can also be re-ordered to be a little more efficient.
ops = [False, False, False, False]
if num < 2:
ops[1] = True
if num <= 1:
ops[2] = True
if num == 1:
ops[0] = True
if num != 1:
ops[3] = False
if any(ops):
for num, op in enumerate(ops):
if op:
print("op %d" % (num+1))
else:
print("Fail")
(Note that in this code, you would never print Fail, since num either is or isn't equal to 1, and both cases are caught.)
Hey everyone im new here and im trying to make a game called HiQ now i got the board drawn and everything and i can click on one of the pieces, but when i do the piece does change color and i get an error in the shell as well (listed below) im not sure why im getting this and i was hoping you guys could give me better insight. Ill provide my code below as well and it is coded in python 3, thank you
builtins.IndexError: list index out of range
boardcirc =[[0,0,0,1,1,1,0,0,0],
[0,0,0,1,1,1,0,0,0],
[0,0,0,1,1,1,0,0,0],
[1,1,1,1,1,1,1,1,1],
[1,1,1,1,2,1,1,1,1],
[1,1,1,1,1,1,1,1,1],
[0,0,0,1,1,1,0,0,0],
[0,0,0,1,1,1,0,0,0],
[0,0,0,1,1,1,0,0,0]]
def HiQ():
splash_screen()
make_board()
def make_board():
make_sqr()
make_circ()
get_click()
def get_click():
global count, boardcirc
while 1!=0:
count = count - 1
displaymessage("Pieces: " + str(count))
where = win.getMouse()
col = where.x//90
row = where.y//90
valid_move(row,col)
make_move(row,col)
def valid_move(row,col):
if boardcirc[row][col] == 0:
return False
if boardcirc[row-1][col] == 1 and boardcirc[row-2][col] == 1:
return True
if boardcirc[row+1][col] == 1 and boardcirc[row+2][col] == 1:
return True
if boardcirc[row][col-1] == 1 and boardcirc[row][col-2] == 1:
return True
if boardcirc[row][col+1] == 1 and boardcirc[row][col+2] == 1:
return True
def make_move(row,col):
while valid_move(row,col) == True:
col = (col*85)+42
row = (row*85)+42
circ = Circle(Point(col,row),35)
circ.setFill("white")
circ.draw(win)
thats everything that applies to the error
For your valid_move(row,col), you can't have all those if statements.
Instead of doing this, use elif's after the initial if statement, and don't forget to write an else statement
if boardcirc[row][col] == 0:
return False
if boardcirc[row-1][col] == 1 and boardcirc[row-2][col] == 1:
return True
elif boardcirc[row+1][col] == 1 and boardcirc[row+2][col] == 1:
return True
elif boardcirc[row][col-1] == 1 and boardcirc[row][col-2] == 1:
return True
elif boardcirc[row][col+1] == 1 and boardcirc[row][col+2] == 1:
return True
else:
return False
I have to create a function according to some specific instructions. It is designed to check if a number is prime or not. I know there are different ways to do this, and I am sorry if this is a stupid question. I am new to programming.
Anyways, here are the instructions (pseudocode):
Function isPrime(n)
if n=1 then return false
else
if n<4 then return true #2 and 3 are prime
else
if n mod 2=0 then return false
else
if n<9 then return true #we have already excluded 4,6 and 8.
else
if n mod 3=0 then return false
else
r=floor( sqrt(n) ) #sqrt(n) rounded to the greatest integer r so that r*r<=n
f=5
while f<=r
if n mod f=0 then return false (and step out of the function)
if n mod(f+2)=0 then return false (and step out of the function)
f=f+6
endwhile
return true (in all other cases)
End Function
and here is my code so far: (Edited for correct indentation)
def isrime(n):
if n == 1:
return False
elif n < 4:
return True
elif n & 2 == 0:
return False
elif n<9:
return True
elif n %3 == 0:
return False
????? (Don't know what to write)
else:
return True
Thanks in advance
def isPrime(n):
if n == 1:
return False
elif n < 4:
return True
elif n % 2 == 0: # needs modulo not &
return False
elif n < 9:
return True
elif n % 3 == 0:
return False
else:
r = n**.5
f = 5
while f <= r:
if n % f== 0:
return False #(and step out of the function)
if n % (f+2)== 0:
return False# (and step out of the function)
f+=6
return True
def is_prime(n):
if n <= 3:
if n <= 1:
return False
return True
if not n%2 or not n%3:
return False
for i in range(5, int(n**0.5) + 1, 6):
if not n%i or not n%(i + 2):
return False
return True