2013-08-04 22 views
-2

这是Python。Python:我的程序不能按照预期工作?

你好,我的程序运行完美,编译没有错误,但是当我运行下面的第二个选项时,它什么都不做。 '递归'函数将不会运行,并且它所打印的所有内容都是无限的“选择一个选项:”。我在某个地方缺少缩进还是忘了包含某些东西?

selection = 0 

while selection != 3: 

    #This is printed out infinitely. 
    selection = int(input("Select an option: ")) 

#Second Option 
elif selection == 2: 
    def recursion(): 
     myString = str(input("Enter your string: ")) 
     if not myString.replace('()', ''): 
      print("The string is even.") 
     else: 
      print("The string is not even.") 
+1

此代码不能运行;它在'elif'上给出了'SyntaxError:invalid syntax'。 – Blair

回答

1

我想你在这里误解了一些结构。

while selection != 3表示下面的代码(和缩进)将继续重复运行,直到selection == 3

elif不在结构中使用。这用于条件结构(即if/elif/else)。

你大概意思有:

selection = 0 
def recursion(): 
    myString = str(input("Enter your string: ")) 
    if not myString.replace('()', ''): 
     print("The string is even.") 
    else: 
     print("The string is not even.") 

while selection != 3: 
    selection = int(input("Select an option: ")) 
    if selection == 2: 
     recursion()