2016-11-25 22 views
-2

這裏是我的腳本:無效的語法,而真正的statemtn

def makeithappen(): 
    word="" 
    while True: 
     try: 
      word=inputText() 
     except: 
      print("Something happened inputText") 
     else: 
      if len(word)>0: 
       break 
      elif word!=str: 
       break 

出於某種原因,但我得到一個無效的語法錯誤,我不知道爲什麼。

+1

你的代碼沒有意義。你想要的輸出到底是什麼? – Inconnu

+1

就這麼你知道,stackoverflow並不關心你的最後期限。 –

+0

哪一行?你可以發佈堆棧跟蹤嗎? – tdelaney

回答

0
def makeithappen(): 
    word="" 
    while True: 
     try: 
      word=input() #is this supposed to be input()? 
     except: 
      print("Something happened inputText") 
     else: 
      if len(word)>0: 
       break 
      elif isinstance(word, basestring): #I never got the logic behind it 
       break 

我認爲這是你想要做的。如果輸入的文本有效(長度大於0)並且輸入類型不是str(對於python3,總是false),它將退出。

+1

類型檢查的規範方式是isinstance(word,str)或更好的isinstance(word,basestring)。 –

+0

改變了它。謝謝,我完全忘記了'isinstance()' – Prajwal

0
#!/usr/bin/python 
# -*- coding: utf-8 -*- 

def makeithappen(): 
    word="" 
    while True: 
     try: 
      word=raw_input("Go:") 
     except: 
      print("Something happened inputText") 
     else: 
      if len(word)>0: 
       print("Hello!!") 
      elif word!=str: 
       print("Bye!!") 
       break 
makeithappen()