2013-04-30 32 views
-1

這讓我感到困擾很長一段時間,我終於決定發佈這個求助。我需要儘快回答。意外的unindent錯誤Python 3.3

def _define_username() -> str: 
''' Asks for username''' 
while True: 
    username = input('Login: ').strip() 
    if len(username) > 1: 
     return username 
    else: 
     print('Sorry, ' + username + ' is not a valid host') 
+0

我發現了一個'IndentationError:預計縮進block'指向2號線,這應該提醒你下面的問題@datguy答案。 – fgb 2013-04-30 06:57:46

+0

確保您不要在編輯器中使用選項卡! Python使用縮進來識別代碼塊(條件,循環,函數,類)。根據用戶設置,選項卡可以擴展到1,2,4或任意數量的字符。標籤與依賴縮進級別的語法組合將導致嚴重破壞。當前所有編輯器都可以設置爲強制替換帶有給定數量空格的製表符的模式。確保它在使用中。然後可視化代碼中的所有選項卡,並將其替換爲空格。再次,大多數編輯可以幫助你。 – cfi 2013-04-30 08:57:53

回答

1

Python對縮進很挑剔。嘗試縮進函數體:

def _define_username() -> str: 
    ''' Asks for username''' 
    while True: 
     username = input('Login: ').strip() 
     if len(username) > 1: 
      return username 
     else: 
      print('Sorry, ' + username + ' is not a valid host') 
+0

不幸的是,它沒有工作 – user2334392 2013-04-30 04:01:16

+0

你需要縮進線下'而真:' – datguy 2013-04-30 04:04:46

+0

錯誤顯示了在第一行,冒號 – user2334392 2013-04-30 04:42:42