如何使变量在函数中起作用?
how to make a variable work in a function?
我想创建一个存储位置的函数,但是函数中的位置变量变暗了,当 运行 它说:
UnboundLocalError:赋值前引用的局部变量'favPlayer2'
我刚开始使用函数所以..谢谢你的帮助
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()
缩进一些 if 块以确保变量已定义
您可以提供一个全局变量,或传递 return 来自函数的值。但是既然你一直没有位置,我就用一个全局变量。
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)
输出:
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
我想创建一个存储位置的函数,但是函数中的位置变量变暗了,当 运行 它说: UnboundLocalError:赋值前引用的局部变量'favPlayer2' 我刚开始使用函数所以..谢谢你的帮助
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()
缩进一些 if 块以确保变量已定义
您可以提供一个全局变量,或传递 return 来自函数的值。但是既然你一直没有位置,我就用一个全局变量。
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)
输出:
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