0

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()
Kshitij Saraogi
7,6999 gold badges49 silver badges75 bronze badges
asked Dec 3, 2015 at 16:39
3
  • You need to actually return the team value then use it later. Commented Dec 3, 2015 at 16:44
  • Thank you how would i do that just by calling it at the start of the stake function with 'Team()'? Commented Dec 3, 2015 at 16:45
  • It looks like you're mixing two things: You're treating team and stake as globals (but have not declared them as such, so it's not working), and you're also trying to return them (but only when recursing, so again it doesn't work). If you want them to be global, then declare them global inside the functions. Commented Dec 3, 2015 at 16:46

2 Answers 2

1

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
answered Dec 3, 2015 at 16:47
1

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.

answered Dec 3, 2015 at 17:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.