2016-02-26 76 views
-3

我找不到任何專門針對我的問題的東西。 Python期望在raw_input內真正地使用縮進塊。Python期望raw_input內部的縮進

relations = 0 
firstNumber = 0 
secondNumber = 0 
def additionProblem(x, y): 
    answer = x + y 
    return answer 
def askingTheNumbers(self): 
    firstNumber = int(raw_input("What is the first number?")) 
    secondNumber = int(raw_input("What is the second number?")) 
import datetime 
now = datetime.datetime.now 
while True: 
    firstResponse = raw_input("Hi! I am a really bad AI created by none other than (names removed for privacy. How was your day?") 
while True: 
    if firstResponse.lower() in ['good', 'great', 'amazing'] and relations == 0: 
    artificialResponse = raw_input("Wow! That's great! I can do very limited things right now. All I'm programmed to do is math and the time.") 
     relations = 1 
    if relations == 1: 
     artificialResponse = raw_input("Would you like me to do anything else?") 
    if artificialResponse.lower() in [time]: 
     whatTimeIsIt = raw_input("Would you like to know the year, month, day, hour, minute, second, or microsecond? You can choose multiple if you want.") 
     if whatTimeIsIt.lower() in ['year']: 
      print "Current year: %d" % now.year 
     if whatTimeIsIt.lower() in ['month']: 
      print "Current month: %d" % now.month 
     if whatTimeIsIt.lower() in ['day']: 
      print "Current day: %d" % now.day 
     if whatTimeIsIt.lower() in ['hour']: 
      print "Current hour: %d" % now.hour 
     if whatTimeIsIt.lower() in ['minute']: 
      print "Current minute: %d" % now.minute 
     if whatTimeIsIt.lower() in ['second']: 
      print "Current minute: %d" % now.second 
     if whatTimeIsIt.lower() in ['microsecond']: 
      print "Current microsecond (though I don't know why you care): %d" % now.microsecond 
    elif artificialResponse.lower() in ['math']: 
     mathProblem = raw_input("Great! Would you like me to do addition, multiplication, division, or multiplication?") 
     if mathProblem.lower() in ['addition']: 
      askingTheNumbers(pass) 
      additionAnswer = additionProblem(firstNumber, secondNumber) 
      print "The answer is", additionAnswer 

在第16行,它期望程序有一個縮進塊,但我絕對不知道爲什麼會這樣。這也是我的第一個真正的Python項目,所以告訴我,如果你看到任何可以改進的東西。

+2

它不希望在該函數中使用縮進*。整行需要縮進。在行的開頭放四個空格,並觀察發生了什麼。魔法! – zondo

+0

這是XD無法相信的問題,我之前沒有注意到。謝謝! – Thepicheese

+0

所有你需要做的就是像Python告訴你的縮進行16。在Python中讀取Traceback錯誤是調試Python的好方法。 – MarkyPython

回答

0

我運行了你的代碼,並收到第10行而不是第16行的錯誤。第10行需要縮進。我解決了這個問題,運行了代碼,並在第33行收到了一個錯誤。您不能使用pass保留字作爲函數的參數。沒有參數的函數調用只是一個空的參數列表()。我跑你的程序修復這個錯誤後,它開始與消息執行:

Hi! I am a really bad AI created by none other than (names removed for privacy. How was your day?

假設這是你所期望的,都應該與語法錯誤是很好。但可能不是語義上的。我假設你不打算有一行一行無限循環,但其餘的代碼隨後成爲該循環的一部分。

正如他們所說,其餘部分留給學生練習。