I’m having an issue on my 2nd path with checking both team 1 and team 2 against the team list, the 2 solutions I’ve found has required both inputs to be the same to be accepted. Also is a while loop the best way to be going about this? Considering my goal is to not have any typo’s because I would like all the data to be stored later on and accessible. Any advice would be appreciated as I am trying to learn.
team_list = ["diamondbacks", "braves", "orioles", "red sox", "white sox", "cubs", "reds", "indians", "rockies", "tigers", "astros", "royals", "angels", "dodgers", "marlins", "brewers", "twins", "yankees", "mets", "athletics", "phillies", "pirates", "padres", "giants", "mariners", "cardinals", "rays", "rangers", "blue jays", "nationals"] if path == 1: #percentage calculator p = int(input("What %? ")) t = int(input("What is your total? ")) percent = (t * p) / 100 print(percent) if path == 2: #Bet logging while True: found_team = False try: team1 = str(input("Betting Team? ")) team2 = str(input("Opposing Team? ")) except ValueError: for t in team_list: try: if (team1, team2) in team_list: found_team = True except ValueError: if found_team == True: print("accepted") break finally: print("failed")
Advertisement
Answer
I’m having an issue on my 2nd path with checking both team 1 and team 2
Few suggestions:
- Don’t use
try....except
as a replacement forif....else
. Here, only ifValueError
is raised, canexcept ValueError:
catch it and execute the remaining code. (x,y)
creates a tuple. And there is no occurrence oftuple
in the list:
>>> print("Type of (x,y)",type((x,y))) Type of (x,y) <class 'tuple'>
- No need to use
for
loop while usingin
operator to check if a string is in the list. Here is some small snippet you might use.
found_team=True while True: x=input("Betting Team: ") y=input("Opposite Team: ") if x in team_list or y in team_list: print("Oops! One team is already taken") else: found_team=True break print("Done!")
Also is a
while
loop the best way to be going about this?
If you want to constantly ask the user till the right input is entered, yes. Use a while
loop to keep asking for user-input till a correct input is entered.