Bob457678y
Bob457678y

Reputation: 3

If in list function - python

I am having an issue with writing functions to determine if firstly, something is in a list and secondly, in relation to the user having a bank balance.

Essentially what the function needs to do is, allow the user to use their balance to bet on a team and also check if the team they have typed in is in the list of teams.

This is what I have so far if anybody could help me with this. I feel like it may need to be one function for both but can't work it out and other threads have not helped me, thanks.

team_list = ["Team1", "Team2", "Team3"]

def Team():
    team = input("Please choose a team to bet on.")
    if team in team_list:
        print("You have selected",team)
    else:
        print("That team is not in the list please select one in the list")
        return Team()

def Stake():
    bank = float(50.0)
    stake = float(input("Please enter a stake"))
    if stake <= bank:
        print("Your bet has been placed.", stake, "on", team)
        bank -= stake
        print(bank)
    else:
        print("You don't have enough money to place this")
        return Stake()

Team()
Stake()

Upvotes: 0

Views: 1154

Answers (2)

Kshitij Saraogi
Kshitij Saraogi

Reputation: 7609

The scope of team variable used is Team is limited to itself. It cannot be accessed in the Stake function. In order to resolve the problem, here is what you can do:

team_list = ["Team1", "Team2", "Team3"]
bank = 50.0

def Team():
   team = input("Please choose a team to bet on.")
   if team in team_list:
       print("You have selected",team)
       Stake(team)
   else:
       print("That team is not in the list please select one in the list")
       return Team()

def Stake(team):
    bank = float(50.0)
    stake = float(input("Please enter a stake"))
    if stake <= bank:
        print("Your bet has been placed.", stake, "on", team)
        bank -= stake
        print(bank)
    else:
        print("You don't have enough money to place this")
        return Stake(team)
Team()

Moreover,if you want to merge these two functions, you might have to work a bit. However, first - you should understand about variable scoping.
Check the official documentation: variable scoping.
It might be old but it still is relevant.

Upvotes: 1

shuttle87
shuttle87

Reputation: 15924

You need to learn more about variable scope. The team variable is a local variable to the Team function, that means team only exists within the scope defined by Team. That means that in Stake there is no variable called team.

To deal with this you need to pass some variables around using return statements and parameters. First you need to return the team variable from Team:

def Team():
    team = input("Please choose a team to bet on.")
    if team in team_list:
        return team

Then you can use this later, by first storing that return value then passing it as a parameter to Stake:

selected_team = Team()
print("You have selected", selected_team)
Stake(selected_team)

Where Stake is modified so it can take a parameter:

def Stake(team):
    #now team variable *is* accessible through the parameter

Upvotes: 1

Related Questions