-1

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.


shailesh
  • 43
  • 1
  • 7
  • Where does the error occurs ? Could you reduce the code to post only what is involved ? – azro Apr 15 '20 at 17:21
  • You check for `'/'` and `'0'`. What about `'/'` and `'0.0'`? If you want to debug this, just put the `eval` inside a `try` ... `except` and *print the expression* when you get an exception. Also, don't use `eval`, use `ast.literal_eval`, it's safer. – Tom Karzes Apr 15 '20 at 17:30
  • @TomKarzes thanks. This was a great help. – shailesh Apr 15 '20 at 17:52

1 Answers1

1

The problem was that sometimes the Num variable was "0.0" and you just protected for "0" cases. In strings, "0" and "0.0" are not equal. So, the best way to do this, is changing your check_divZero method to:

def check_divZero(Sign, Num):
    if Sign == '/' and float(Num) == 0:  # checks for zeroes.
        return False
João Castilho
  • 487
  • 4
  • 19