简体   繁体   中英

How do you return the result in my code?

#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)

I'm having this problem where the result shown above isn't being returned what do I do? I have been having some trouble with this! I would appreciate the help, this is in Python 2.7.

If your second if evaluates to False the value returned is None since you only return a value within the second if .

Move the return result (un-indent once) outside the second if statement to get a result regardless of what your if statements evaluate to:

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) 

Shift your last line one tab left.

If neither if statement evaluates to True you'll return None.

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')
'''
#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) 

This works like a charm! Thanks for the contributors, you guys helped me understand the concept! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM