2016-01-22 102 views
0

我知道這應該是非常簡單的,但在這一點上,我在智慧的結尾試圖弄清楚這一點。我已經在python中編寫了一個計算器,但由於某種原因,結尾if-else語句只會觸發else段。Python:如果條件似乎得到滿足,但沒有觸發

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語句都會觸發。 (例如,如果我在「再次播放?」中輸入「是」,則會打印: 「是」(< - 來自「再次打印」行) 「沒有」(< - 這是來自「其他:打印‘no'd’)

我不知道如何解決這些問題之一,在這一點上,所以任何幫助,將不勝感激

編輯:謝謝大家。 ,我希望我能查馬克大家幫助我理解關於我做錯了什麼不同的事情。

回答

1

while valid == True:,你永遠不會改變的價值,所以它總是True和循環是無限的。我不明白爲什麼它甚至是一個循環 - 將其更改爲if,就像它上面的塊一樣,它將按預期行事。

另外,在if again == ("yes", "y", "YES", "Yes","yes"):中,將==更改爲in並且其行爲將與預期相同。

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 

你也可以只簡單地設置有效的假的也在你的循環底部。這將工作。

我認爲在這種情況下有效性是不變的。你也寫了while while valid,這意味着它將繼續遍歷循環,直到有效等於false。看起來,在while循環中的這段代碼中,有效值不會切換爲false。

1

while valid == True:也許應該是if valid == True

,併爲您的第二個問題:

if again == ("yes", "y", "YES", "Yes","yes"):也許應該是:

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

你的答案是循環的,因爲

while valid == True: 

更換循環與if聲明


你得到「no'd」因爲

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

這裏給您等同字符串元組,而不是檢查字符串是否包含一個元組內。試試這個:

if again in ("yes", "y", "YES", "Yes""): 
相關問題