True statemtn时语法无效

时间:2016-11-25 06:20:25

标签: python syntax while-loop syntax-error

这是我的剧本:

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

由于某种原因,我得到了无效的语法错误,我不确定原因。

2 个答案:

答案 0 :(得分: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 :(得分: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()