2014-11-05 121 views
0

所以,這裏是我的腳本:我的python腳本有什麼問題?

#Imports 
import decimal 
#variables 
neweq = "neweq" 
on = 1 
#loop 
while on > 0: 
#equasion function 
    def eq(): 
     global b 
     b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit: ") 
     print "" 
     print "" 
     print "" 
     print "" 
     b = float(b) 
     b = '%.3f'%(b) 
     if (b==exit): 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      exit ("Thank you for using me :)") 
#input funcution 
    def inp(): 
     a = input("Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: ") 
     if (a==exit): 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      print "" 
      exit ("Thank you for using me :)") 
     if (a == neweq): 
      print "" 
      print "" 
      a = 0 
      eq() 
      inp() 
     if (b==a): 
      print "" 
      print "Yes, the answer is", a 
      print "" 
      print "" 
      eq() 
     else: 
      print "" 
      print "No, the answer is not", a 
      print "" 
      print "" 
      print "test line", b 
      inp() 
#function calls 
    eq() 
    inp() 

的問題?

Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit: 2.0/4.0 




Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: 1/2 

No, the answer is not 0 


test line 0.500 
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: 102.0 

No, the answer is not 102.0 


test line 0.500 
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: 1.0/2.0 

No, the answer is not 0.5 


test line 0.500 
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: .500 

No, the answer is not 0.5 


test line 0.500 
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: 0 

No, the answer is not 0 


test line 0.500 
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: 4.0/2.0 

No, the answer is not 2.0 


test line 0.500 
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: 2.0/4.0 

No, the answer is not 0.5 


test line 0.500 
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: 0.500 

No, the answer is not 0.5 


test line 0.500 
Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: 

它應該返回'是的,答案是0.5',但是,它不是。與其他幾個類似。我無法弄清楚它有什麼問題,但是,我懷疑它是b = '%.3f'%(b),這就是我需要幫助的地方。

謝謝!

+0

你可以告訴我,什麼輸入將被期望?就像'float/float' – Hackaholic 2014-11-05 04:48:43

+0

@Hackaholic'1.0/3.0','1.0/3.0','2 *(3/4 + 1)'和一些基本的數學例如'1 + 1',並且很快就會'3 *(4/A-3)== 4 /(A-1)'。 – 2014-11-05 04:57:57

+1

與您遇到的直接問題無關的幾個指針:將函數放入循環中實際上每次都重新定義它們 - 您可以將它們帶到最高級別。比較'b == exit'將不起作用 - 首先,b已經是一個數字;第二,你正在比較函數'exit'而不是string'「exit」'。你可以使用實際的布爾值,所以你可能想'on = True'和'while on:'。腳本中還有其他不常用的東西 - 我建議您先從簡單的步驟開始,並在圍繞它們構建更多代碼之前驗證它們是否正常工作。 – viraptor 2014-11-05 05:09:51

回答

1

你去那裏。我編輯了你的代碼。讓我知道如果這是你想要的,我不知道你爲什麼使用print "",我在這裏刪除它,因爲它傷害了我的眼睛! :P。也請不要使用exit作爲用戶的輸入,最好使用「quit」。可在評論中找到解釋。

#Imports 
import decimal 
#variables 
neweq = "neweq" 
on = 1 
#loop 
#equasion function 
def eq(): 
     global b 
     b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit: ") 
     if b == exit: 
      exit ("Thank you for using me :)") 
     else: 
      b = float(b)  ## input converted into float. 
      b = '%.3f'%(b)  ## after this b would be of type string 
      b = float(b)  ## again converting into float to match with "a" in `inp()` 
#input funcution 
def inp(): 
     a = input("Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion: ") 
     if a == exit: 
      exit ("Thank you for using me :)") 
     if a == neweq: 
      a = 0 
      eq() 
      inp() 
     if b == a:   ## if a == b should work now. 
      print "Yes, the answer is", a 
      eq() 
     else: 
      print "No, the answer is not", a 
      print "test line", b 
      inp() 
#function calls 
eq() 
inp() 

如果你使用print""以避免在工作空間雜波然後嘗試這樣

print "\n"*5 ## You have 5 empty lines. Replace the number 5 as per your needs 

這是更加整潔Python的。

+0

我希望它進入一個無限循環,是的,它有一些錯誤,我不想要一個計算器,'測試線'是一個調試幫手。我不想建立一個計算器,我想要一個答案檢查器。很多代碼都是針對意圖的,但是,你設法解決我的問題!謝謝! – 2014-11-05 05:38:54

+0

@ Thecheater887你想讓我陷入無限循環嗎?想要解釋哪裏出錯了? 「print」「''背後的原因是什麼? – 2014-11-05 05:42:20

+0

對不起!有Ninj'd!我現在開始工作了,但是我想要一個循環,所以在每次平衡之後你都不必重新啓動。 'b = float(b)''b ='%.3f'%(b)' 'b = float(b)' 是什麼完成了代碼。 'print'「是這樣的,它不會讓工作空間變得混亂,就像你能看到你在哪裏一樣。儘管您的幫助解決了我的問題,但非常感謝! – 2014-11-05 05:46:20

0

該位看起來可疑,它需要(大概)一個字符串,並試圖將其轉換爲float:

b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit: ") 
    b = float(b) 
+0

我需要將它放在小數點後3位的位置,或者我不需要它是浮點數。你可以建議如何做到這一點沒有浮動? – 2014-11-05 04:43:36

+0

你的腳本是這樣做的:'float(「a ** 3 + 2 * a + 5」)',這會失敗。在嘗試縮短浮點數之前,您需要採取該字符串並以某種方式計算(對數字而不是字符串)。 – 101 2014-11-05 04:46:17

+0

>>>浮動( '3.0/7.0') 回溯(最近通話最後一個): 文件 「」,1號線,在 ValueError異常:無效的文字浮法():3.0/7.0 – Hackaholic 2014-11-05 04:46:57