Python Program Doesn't Execute Any Functions - python

I'm currently learning python for fun. I'm used to coding in C++ and so far it's fairly easy to pickup. I wrote my first program on my own, but for some reason, it doesn't do anything and the functions I wrote won't execute. If I write print statements outside the functions, the statements execute, but it never goes inside the function. Here is my code. Any tips would be much appreciated.
racks = 1000000
sum = 0
def ConsecutivePrime():
primeNum = 0
stack = []
while(StackAdder == False):
primeNum = isPrime(primeNum)
stack.append(primeNum)
StackAdder(stack)
if(StackAdder == True):
print ("Largets Prime: ", sum)
def StackAdder(stack):
for n in stack:
sum += n
if(count < racks):
return False
else:
stack.pop()
return True
def isPrime(primeNum):
isPrime = False
while(isPrime == False):
primeNum += 1
if(primeNum % 2 == 1): #First Checks If Odd
if(primeNum % 3 == 0):
isPrime == False
elif(primeNum % 5 == 0):
isPrime == False
elif(primeNum % 7 == 0):
isPrime == False
elif(primeNum % 9 == 0):
isPrime == False
else:
isPrime == True
if(isPrime == True):
return primeNum
def main():
ConsecutivePrime()
if __name__ == "__main__":
main()

StackAdder is a function; it is neither True nor False, so ConsecutivePrime is called, it just doesn't do anything.

Add these lines at the beginning of your ConsecutivePrime() function, and observe the output:
print(StackAdder == True)
print(StackAdder == False)
You can see False printed twice, right? Surprised? Read the comments on the answer by Scott Hunter. May be that will help a little.
So, your conditionals for both while and if are False.
If what you wanted was to check what value StackAdder() returned, you need to do it like this:
def ConsecutivePrime():
primeNum = 0
stack = []
while(StackAdder(myStack) == False): # define myStack to your needs
primeNum = isPrime(primeNum)
stack.append(primeNum)
StackAdder(stack)
if(StackAdder(myStack) == True):
print ("Largets Prime: ", sum)

There are major problems in the way you structured your code, and I will try to point out the most obvious mistakes in your functions:
1) Regarding the function isPrime(primeNum), this is the correct way of writing it:
def isPrime(primeNum):
isPrime = False
while not isPrime:
primeNum += 1
if(primeNum % 2 == 1): #First Checks If Odd
if(primeNum % 3 == 0):
isPrime == False
elif(primeNum % 5 == 0):
isPrime == False
elif(primeNum % 7 == 0):
isPrime == False
elif(primeNum % 9 == 0):
isPrime == False
else:
isPrime == True
return primeNum
I rewrote while(isPrime == False) as while not isPrime. Also, you don't need the if(isPrime == True) statement, because the while loop will be exited when isPrime == True.
2) In the function StackAdder(stack), you are introducing count, which was not defined before. Maybe you wanted sum instead? Or maybe you were trying to use the count() method (which returns the count of how many times an obj occurs in a list?)
3) In the function ConsecutivePrime(), Stackadder is a function, so your code should be: while(StackAdder(stack) == False)

Related

Blank List, Return False

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

Why is this function with a return value returning none?

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)

Check first condition in IF and test in the next statement too in Python

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.)

List index out of range when coding a valid move for board game

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

Function to check prime according to specfic instructions

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

Categories