I want to make a function that store the position, but the position variable in the function goes dark and when run it says: UnboundLocalError: local variable 'favPlayer2' referenced before assignment I just start out with function so.. thanks for your help
def position():
if weight > 80 and height > 190:
position = 'Center'
print('Your position is: Center')
if weight > 80 and height < 190:
print('Your position is: Power Forward or Small Forward')
favPlayer = str.lower(input("Favorite player in the list (Lebron James, Jayson Tatum): "))
while favPlayer != 'lebron james' and favPlayer != 'jayson tatum':
favPlayer = str.lower(input('Please enter the name correctly: '))
if favPlayer == 'jayson tatum':
position = 'Small Forward'
print('Your position is Small Forward')
elif favPlayer == 'lebron james':
position = 'Power Forward'
print('Your position is Power Forward')
if weight < 80 and height < 190:
print('Your position is Point Guard or Shooting Guard')
favPlayer2 = str.lower(input("Favorite player in the list (Chris Paul, Klay Thompson): "))
while favPlayer2 != 'chris paul' and favPlayer2 != 'klay thompson':
favPlayer2 = str.lower(input('Please enter the name correctly: '))
if favPlayer2 == 'chris paul':
position = 'Point Guard'
print('Your position is Point Guard')
if favPlayer2 == 'klay thompson':
position = 'Shooting Guard'
print('Your position is Shooting Guard')
position()
CodePudding user response:
Indent some if blocks to make sure the variable is defined
You can give a global variable, or pass return the value from function. But since you don't have position all the time, I will use a global variable.
weight = int(input("Enter your weight: "))
height = int(input("Enter your height: "))
position = None
def get_position():
global position
if weight > 80 and height > 190:
position = 'Center'
print('Your position is: Center')
if weight > 80 and height < 190:
print('Your position is: Power Forward or Small Forward')
favPlayer = str.lower(input("Favorite player in the list (Lebron James, Jayson Tatum): "))
while favPlayer != 'lebron james' and favPlayer != 'jayson tatum':
favPlayer = str.lower(input('Please enter the name correctly: '))
# indented to make sure 'favPlayer' is defined before the if statement
if favPlayer == 'jayson tatum':
position = 'Small Forward'
print('Your position is Small Forward')
elif favPlayer == 'lebron james':
position = 'Power Forward'
print('Your position is Power Forward')
if weight < 80 and height < 190:
print('Your position is Point Guard or Shooting Guard')
favPlayer2 = str.lower(input("Favorite player in the list (Chris Paul, Klay Thompson): "))
while favPlayer2 != 'chris paul' and favPlayer2 != 'klay thompson':
favPlayer2 = str.lower(input('Please enter the name correctly: '))
if favPlayer2 == 'chris paul':
position = 'Point Guard'
print('Your position is Point Guard')
if favPlayer2 == 'klay thompson':
position = 'Shooting Guard'
print('Your position is Shooting Guard')
get_position()
print(position)
output:
Enter your weight: 10
Enter your height: 29
Your position is Point Guard or Shooting Guard
Favorite player in the list (Chris Paul, Klay Thompson): Chris Paul
Your position is Point Guard