I'm a really amateur programmer trying to learn python by myself. Most of this code is googled. I have no idea what I'm doing.
I am getting the error "float division by zero"
I was trying to make a countdown solver. Countdown is a game show and the rules with the method I have been following loosely are here.
Here's how the expressions are generated: All distinct permutations of the numbers and the function product on the operators is done. They are then arranged to form meaningful postfix expressions. Eg: with numbers 4, 4 and 10 these are the possible expressions
[['10', '4', '+', '4', '+'], ['10', '4', '4', '+', '+']]
[['10', '4', '+', '4', '-'], ['10', '4', '4', '+', '-']]
[['10', '4', '+', '4', '*'], ['10', '4', '4', '+', '*']]
[['10', '4', '+', '4', '/'], ['10', '4', '4', '+', '/']]
[['10', '4', '-', '4', '+'], ['10', '4', '4', '-', '+']]
[['10', '4', '-', '4', '-'], ['10', '4', '4', '-', '-']]
[['10', '4', '-', '4', '*'], ['10', '4', '4', '-', '*']]
[['10', '4', '-', '4', '/'], ['10', '4', '4', '-', '/']]
[['10', '4', '*', '4', '+'], ['10', '4', '4', '*', '+']]
[['10', '4', '*', '4', '-'], ['10', '4', '4', '*', '-']]
[['10', '4', '*', '4', '*'], ['10', '4', '4', '*', '*']]
[['10', '4', '*', '4', '/'], ['10', '4', '4', '*', '/']]
[['10', '4', '/', '4', '+'], ['10', '4', '4', '/', '+']]
[['10', '4', '/', '4', '-'], ['10', '4', '4', '/', '-']]
[['10', '4', '/', '4', '*'], ['10', '4', '4', '/', '*']]
[['10', '4', '/', '4', '/'], ['10', '4', '4', '/', '/']]
[['4', '10', '+', '4', '+'], ['4', '10', '4', '+', '+']]
[['4', '10', '+', '4', '-'], ['4', '10', '4', '+', '-']]
[['4', '10', '+', '4', '*'], ['4', '10', '4', '+', '*']]
[['4', '10', '+', '4', '/'], ['4', '10', '4', '+', '/']]
[['4', '10', '-', '4', '+'], ['4', '10', '4', '-', '+']]
[['4', '10', '-', '4', '-'], ['4', '10', '4', '-', '-']]
[['4', '10', '-', '4', '*'], ['4', '10', '4', '-', '*']]
[['4', '10', '-', '4', '/'], ['4', '10', '4', '-', '/']]
[['4', '10', '*', '4', '+'], ['4', '10', '4', '*', '+']]
[['4', '10', '*', '4', '-'], ['4', '10', '4', '*', '-']]
[['4', '10', '*', '4', '*'], ['4', '10', '4', '*', '*']]
[['4', '10', '*', '4', '/'], ['4', '10', '4', '*', '/']]
[['4', '10', '/', '4', '+'], ['4', '10', '4', '/', '+']]
[['4', '10', '/', '4', '-'], ['4', '10', '4', '/', '-']]
[['4', '10', '/', '4', '*'], ['4', '10', '4', '/', '*']]
[['4', '10', '/', '4', '/'], ['4', '10', '4', '/', '/']]
[['4', '4', '+', '10', '+'], ['4', '4', '10', '+', '+']]
[['4', '4', '+', '10', '-'], ['4', '4', '10', '+', '-']]
[['4', '4', '+', '10', '*'], ['4', '4', '10', '+', '*']]
[['4', '4', '+', '10', '/'], ['4', '4', '10', '+', '/']]
[['4', '4', '-', '10', '+'], ['4', '4', '10', '-', '+']]
[['4', '4', '-', '10', '-'], ['4', '4', '10', '-', '-']]
[['4', '4', '-', '10', '*'], ['4', '4', '10', '-', '*']]
[['4', '4', '-', '10', '/'], ['4', '4', '10', '-', '/']]
[['4', '4', '*', '10', '+'], ['4', '4', '10', '*', '+']]
[['4', '4', '*', '10', '-'], ['4', '4', '10', '*', '-']]
[['4', '4', '*', '10', '*'], ['4', '4', '10', '*', '*']]
[['4', '4', '*', '10', '/'], ['4', '4', '10', '*', '/']]
[['4', '4', '/', '10', '+'], ['4', '4', '10', '/', '+']]
[['4', '4', '/', '10', '-'], ['4', '4', '10', '/', '-']]
[['4', '4', '/', '10', '*'], ['4', '4', '10', '/', '*']]
[['4', '4', '/', '10', '/'], ['4', '4', '10', '/', '/']]
Help me Lords of Stack Exchange
print_numb=['4','4','10','5']
target=220
operators=['+', '-','*','/']
Example_Expression=['10','5','4','4','-','/','+'] #Exact form expressions are input
#Check if Solutions follow Rules of Countdown or Divide by Zero
def check_divZero(Sign, Num): #problem area probably
if Sign =='/' and Num == '0':
return False
def check_rules(Sol):
if Sol < 0:
return False
elif isinstance(Sol, float) == True and Sol.is_integer() == False:
return False
#Function to Calculate Postfix and Check Target
def postfix_eval (Some_Expression, Some_Target):
stack=[]
for i in Some_Expression:
if i not in operators:
stack.append(i)
elif i in operators:
num2=stack.pop()
num1=stack.pop()
exp=num1+i+num2
if check_divZero(i, num2) == False:
break
sol=eval(exp)
if check_rules(sol) == False:
break
stack.append(str(sol))
if float(stack[-1]) == Some_Target:
return Some_Expression
postfix_eval(Example_Expression, target) #this does not give an error. IDK what kind of expressions do.