vendredi 22 janvier 2016

Python: If condition appears to be met but isn't triggering

I know this seems like it should be very simple, but at this point I'm at my wit's end trying to figure this out. I've coded up a calculator in python, but for some reason the ending if-else statement is only firing the else segment.

import sys
import re

#setting values
x = 0
n = '+'
y = 0
#valid input flag
valid = True
#continue operations flag
run = True
again = "k"

#addition function
def add(x, y):
    return x + y

#subtraction function
def subtract(x, y):
    return x - y

#multiplication function
def multiply(x, y):
    return x * y

#division function
def divide(x, y):
    return x / y

#continuation loop
while run == True:
    #Prompt for and accept input
    equation = raw_input("Please insert a function in the form of 'operand' 'operator' 'operand' (x + y): ")
    equation.strip()
    #Divide input into 3 parts by spaces
    pieces = re.split('\s+', equation)

    #set part 1 = x as float
    x = pieces[0]
    try:
        x = float(x)
    except:
        print "x must be a number"
        valid = False

    #set part 2 = operator
    if valid == True:
        try:
            n = pieces[1]
        except:
            print "Please use valid formating (x [] y)."
            valid = False

    #set part 3 = y as float
    if valid == True:
        y = pieces[2]
        try:
            y = float(y)
        except:
            print "y must be a number"
            valid = False

    #If input is valid, do requested calculations
    while valid == True:
        if n == '+' :
            print equation + " =", add(x,y)

        elif n == '-' :
            print equation, " =", subtract(x,y)

        elif n == '*' :
            print equation, "*", y, " =", multiply(x,y)

        elif n == '/' :
            if y == 0:
                print "You cannot divide by zero."

            else:
                print equation, " =", divide(x,y)

        else:
            print "Please use an appropriate operator ( + - * / )."

#play again
    again = raw_input("Play again? ")
    print again

    if again == ("yes", "y", "YES", "Yes","yes"):
        run = True
        print "yes'd"

    else:
        print "no'd"
        run = False

When I run this code, I get two different problems: If I enter a valid input (ie: 2 + 2), then my output is

"2 + 2 = 4.0"

"2 + 2 = 4.0"

"2 + 2 = 4.0"

repeating forever.

If I enter an invalid input, I get the "Play again? " Prompt, but no matter what I enter, the else statement fires. (for instance, in the case that I enter "yes" into "Play again? ", it will print: "yes" (<-- this is from "print again" line ) "no'd" (<-- this is from "else: print "no'd" )

I dont know how to solve either of these problems at this point, so any help would be greatly appreciated.

Edit: Thank you everyone, I wish I could check mark all of you for helping me understand different things about what I did wrong.

Aucun commentaire:

Enregistrer un commentaire