2016-03-03 90 views
-3

我想比较在我的第一个函数中检索到的两个字符串值,但它不起作用。它一直告诉我'无效的语法',并将我的光标移动到elif行。比较2个字符串值,不起作用!?

这是我的计划......

def find_number_of_service(): 
    with open('TheData.txt', 'r') as data_file: 
     data = data_file.read() 

    countS = data.count('S') 
    countW = data.count('W') 

    print ("There are " + str(countS) + " S's") 
    print ("There are " + str(countW) + " W's") 
    return 


def find_popular_service(): 
    if (countS) > (countW): 
     print ("The most used service to buy tickets was the school.") 
     elif print ("The most used service to buy tickets was the website.") 

     return 

#Main program 
find_number_of_service() 
find_popular_service() 

预先感谢您。

+0

你有太多的错误就更不用说了。你的缩进是关闭的,你使用了没有条件和冒号的elif,你没有在你的函数中定义countS和countW等等。 – Selcuk

+0

我会建议您优化您的问题,更具特色,并只保留所需的信息 - 这样您可以更快地得到答案 – Farside

回答

0

问题在第二个功能elif你没有正确定义应该是elif <condition>:

这里工作功能

def find_popular_service(): 
    if (countS) > (countW): 
     print ("The most used service to buy tickets was the school.") 
    else: 
     print ("The most used service to buy tickets was the website.") 
    return 
0
  • 您不给elif一个条件。 elif表示otherwise if...。也许你的意思是else

  • elif应符合if

  • print ...应该在一个新的生产线。