Python Forum

Full Version: How to select between 2 variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi - super noob here.... so thanks in advance for any help on this.

I am trying to write a script for sports betting, just for fun...

I am getting stuck on how to have the user select the team that is favored to win.

Here is current code:
#Getting the final score of each team
team1 = int(input("Team1 score?"))
team2 = int(input("Team2 score?"))

#Getting the spread and favorite
spread = int(input("Spread?"))
fav = int(input("Who is favored?", )) <-- this where i'm stuck... how to have the user select the team that is favored between
team1 adn team 2
The user must input a team. I don't think there is any reason for it to be an int.

For a real game the user would select using the team name, or some keyboard shortcut for the team.
teams = {"1": "Avians", "2": "Mammals"}
print("Who is favored")
for key, name in teams.items():
    print(key, name)
favored = input("? ")
print("You chose", teams[favored])
Something like this needs a better data structure than a bunch of unrelated variables. I would make a class. A dataclass in particular.
from dataclasses import dataclass


@dataclass
class Game:
    teams: tuple[str, str]
    scores: tuple[int, int]
    spread: int
    favored: int


def bookie(*teams):
    scores = tuple(int(input(f"{team} score: ")) for team in teams)
    spread = int(input("Spread: "))
    print("Who is favored")
    for index, team in enumerate(teams, start=1):
        print(index, team)
    favored = int(input(": ")) - 1
    return Game(teams, scores, spread, favored)


games = (bookie("Avians", "Mammals"), bookie("Aquatics", "Geology"), bookie("Weather", "Occupations"))
print(*games, sep="\n")
Running the program:
Output:
Avians score: 7 Mammals score: 13 Spread: 2 Who is favored 1 Avians 2 Mammals : 2 Aquatics score: 3 Geology score: 52 Spread: 11 Who is favored 1 Aquatics 2 Geology : 2 Weather score: 8 Occupations score: 0 Spread: 7 Who is favored 1 Weather 2 Occupations : 1 Game(teams=('Avians', 'Mammals'), scores=(7, 13), spread=2, favored=1) Game(teams=('Aquatics', 'Geology'), scores=(3, 52), spread=11, favored=1) Game(teams=('Weather', 'Occupations'), scores=(8, 0), spread=7, favored=0)