True statemtn时语法无效

2024-09-30 01:28:50 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的剧本:

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

但是由于某些原因,我得到了一个无效的语法错误,我不知道为什么。在


Tags: trueifdefelsesomethingwordprinttry
2条回答
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),那么它将退出。在

#!/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()

相关问题 更多 >

    热门问题