Python:如果条件似乎已满足但未触发

时间:2016-01-22 22:22:27

标签: python if-statement infinite-loop

我知道这看起来应该很简单,但在这一点上,我正在努力解决这个问题。我在python中编写了一个计算器,但由于某种原因,结尾的if-else语句只触发了else段。

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

当我运行此代码时,我遇到两个不同的问题: 如果我输入有效输入(即:2 + 2),那么我的输出是

“2 + 2 = 4.0”

“2 + 2 = 4.0”

“2 + 2 = 4.0”

永远重复。

如果我输入了无效的输入,我会“再次播放?”提示,但是 无论我输入什么,else语句都会触发。 (例如,如果我在“再次播放?”中输入“是”,它将打印: “是”(< - 这是来自“再次打印”行) “没有”(< - 这是来自“else:print”no'd“)

此时我不知道如何解决这些问题,所以非常感谢任何帮助。

编辑:谢谢大家,我希望能够帮助我了解所有关于我做错的事情。

4 个答案:

答案 0 :(得分:1)

GET中,您永远不会更改while valid == True:的值,因此它总是valid并且循环是无限的。我不明白为什么它甚至是一个循环 - 将其更改为True,就像上面的块一样,它将按预期运行。

此外,在if中,将if again == ("yes", "y", "YES", "Yes","yes"):更改为==,它将按预期运行。

答案 1 :(得分:1)

也许您应该替换此代码:

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 ( + - * / )."

有了......

if valid:

或者...

while valid == True:
    # Insert your previous code here.
    break

您也可以简单地在循环底部将有效设置为false。那会有效。

我认为在这种情况下,有效性始终如一。你也写了有效是真的,这意味着它将继续迭代循环,直到有效等于假。看来,在while循环中的这段代码中,有效的isn不会被切换为false。

答案 2 :(得分:1)

while valid == True:应该是if valid == True

和你的第二个问题:

if again == ("yes", "y", "YES", "Yes","yes"):应该是:

again = again.lower();
if again == "yes" or again == "y":

答案 3 :(得分:1)

由于

,您的答案正在循环播放
while valid == True:

用if语句替换循环

你得到了"没有' d"因为

if again == ("yes", "y", "YES", "Yes", "yes"):

在这里,您将字符串与元组等同,而不是检查字符串是否包含在元组中。试试这个:

if again in ("yes", "y", "YES", "Yes""):