0

I'm trying to write a program where the user types in two football teams. The teams can't be equal. The problem I have is that my program writes my error message multiple times(everytime it doesn't find it in the list). Is there anyway to get around this without completely rewrite the code??

teamlist = ["arsenal", "liverpool", "manchester", "newcastle"]

def finding_team(home_or_away):
    while True:
        user_input = input("Type in " + home_or_away + " team: " )
        for team in teamlist:
            if team.upper() == user_input.upper():
                return team
            else:
                print("Didn't get that, try again")
                continue
def team_input():
    print()
    hometeam = finding_team("HOME")
    awayteam = finding_team("AWAY")
    while hometeam == awayteam:
        print("The away team can't be the same as the home team!")
        awayteam = finding_team("AWAY")
    return(hometeam, awayteam)


the_teams = team_input()
print("The teams you typed in are ", the_teams)
0

2 Answers 2

1
teamlist = ["arsenal", "liverpool", "manchester", "newcastle"]

def finding_team(home_or_away):
    while True:
        team = input("Type in " + home_or_away + " team: " )
        if team.lower() in teamlist:
            return team
        else:
            print("Didn't get that, try again")

def team_input():
    print()
    hometeam = finding_team("HOME")
    awayteam = finding_team("AWAY")
    while hometeam == awayteam:
        print("The away team can't be the same as the home team!")
        awayteam = finding_team("AWAY")
    return(hometeam, awayteam)

the_teams = team_input()
print("The teams you typed in are ", the_teams)

I changed it so that the user's input (stored in team) is checked against all items in teamlist in a single conditional statement.

Type in HOME team: foo
Didn't get that, try again
Type in HOME team: arsenal
Type in AWAY team: arsenal
The away team can't be the same as the home team!
Type in AWAY team: manchester
The teams you typed in are  ('arsenal', 'manchester')
>>> 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it worked! But I also want the program to except input with upper/lowercase letters. I tries to solve this with "if team.upper() == user_input.upper()" but now I can't use that anymore.
@PeterNydahl This code should work just fine if the user enters e.g. "Arsenal" instead of "arsenal" if that's what you're asking. All values you entered in teamlist are lower-case, and the user's input will be coerced to lowercase through .lower()
Yes it works, but let's say that the teams names in the list begins with upper-case(Arsenal, Manchester etc), how do I fix that? Sorry I forgot to be clear about this in the question!
You could either just ensure all items in teamlist are lower-case or you can replace if team.lower() in teamlist: with if team.lower() in [x.lower() for x in teamlist]:.
1

If you only need to check if your teams are in teamliste, you don't need that for loop.

teamlist = ["arsenal", "liverpool", "manchester", "newcastle"]

def finding_team(home_or_away):
    while True:
        user_input = input("Type in " + home_or_away + " team: " )
        if user_input.lower() in teamlist:
            return user_input.upper()
        else:
            print("Didn't get that, try again")

def team_input():
    print()
    hometeam = finding_team("HOME")
    awayteam = finding_team("AWAY")
    while hometeam == awayteam:
        print("The away team can't be the same as the home team!")
        awayteam = finding_team("AWAY")
    return(hometeam, awayteam)


the_teams = team_input()
print("The teams you typed in are ", the_teams)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.