你如何在我的代码中返回结果?

时间:2015-11-11 00:31:14

标签: python python-2.7 syntax return

#Soccer Matchup Possibilities 
def soccermatchups(team1,team2):
    result=" "
    if (team1== "Brazil" and team2== "Canada"):
        result= "These teams could play each other in the World Cup!"
    if (team1== " England" and team2== "Netherlands"):
        result= "These teams could play each other in both Cups!"
        return(result)

我遇到这个问题,上面显示的结果没有被退回,我该怎么办?我一直遇到这个问题!我很感激帮助,这是在Python 2.7中。

4 个答案:

答案 0 :(得分:1)

如果您的第二个if评估为False,则返回的值为None,因为您只在第二个if 中返回值。< / p>

return result(非缩进一次)移到第二个if语句之外以获得结果,无论您的if语句评估为什么:

def soccermatchups(team1, team2):
    result=" "
    if (team1== "Brazil" and team2== "Canada"):
        result= "These teams could play each other in the World Cup!"
    if (team1== " England" and team2== "Netherlands"):
        result= "These teams could play each other in both Cups!"

    # return value either way
    return(result) 

答案 1 :(得分:0)

将最后一行向左移动一个标签。

如果if语句的计算结果均为True,则返回None。

答案 2 :(得分:0)

def soccermatchups(team1,team2):
    if (team1== "Brazil" and team2== "Canada"):
        print "These teams could play each other in the World Cup!"
    if (team1== "England" and team2== "Netherlands"):
        print "These teams could play each other in both Cups!"
    return False


soccermatchups('Brazil', 'Canada')
soccermatchups('England', 'Netherlands')

答案 3 :(得分:0)

'''
#Soccer Match-ups Program
#Author: Coleton Ishmael
#Class: ICS3U
#Date: 11/10/15
'''
#Soccer Matchup Possibilities 
def soccermatchups(team1,team2):
    result="They cannot play each other in either the Euro or World Cup."
    if (team1== " Brazil" and team2== " Canada"):
        result= "They can play in the World Cup!"
    if (team1== " England" and team2== " Netherlands"):
        result= "They can play in both the Euro and World Cup!"
    return (result)

'''
Main Program
@parm Brian
@parm Alyssa
@return result
'''
# Get User's team selection
team1= raw_input (" Enter your first team:")
team2= raw_input (" Enter your second team:")


#Output
print soccermatchups(team1,team2) 

这就像一个魅力!感谢贡献者,你们帮助我理解了这个概念! :)