未绑定本地错误,我不知道如何修复
问题描述:
def perd():
Personaldetails_file = open("Personaldetails_file.txt", "w")
Personaldetails_file = open("Personaldetails_file.txt", "a")
pd = input("Are you a new user?")
if pd == "yes":
print ("Add your details")
####################################
age = int(input("how old are you?"))
DOB = input("Date of birth:")
gender = input("Gender:")
weight = int(input("weight in kg:"))
height = int(input("height in cm:"))
Personaldetails = (age, DOB, gender, weight, height)
Personaldetails_file.write(str(Personaldetails))
Personaldetails_file.close()
#######################################
def td():
choice = input("which trainning event would you like to access?\n1.swimming\n2.cycling\n3.running\nplease type in the number before the event of which you want to choose\n")
if choice == "1":
Swimming_file= open("Swimming_file.txt", "w")
totaldistance = input("what was the total distance you swam in meters?")
totaltime = input("how long did you swim for in minutes?")
speed = totaldistance/totaltime
print ("on average you where running at a speed of", speed, "mps\nyou can look at the tables to see how many calouries you have burnt")
total = (totaldistance, totaltime, speed)
Swimming_file.write(str(total))
Swimming_file.close()
elif choice == "3":
Running_file= open("Running_file.txt", "w")
totaldistanceR = int(input("what was the total distance you ran in KM?"))
totaltimeR = int(input("how long did you run for in minutes?"))
totaltimeR1 = 60/totaltimeR
speedR1 = totaldistanceR/totaltimeR1
calburn = (speedR1 * 95)/(60/totaltimeR1)
print ("\nThe records have been saved")
print ("\non average you where running at a speed of", speedR1, "KMph\nyou burnt",calburn," calouries\n")
totalR = (totaldistanceR, totaltimeR, speedR1, calburn)
Running_file.write(str(totalR))
Running_file.close()
##############################################################
elif choice == "2":
Cycling_file= open("Cycling_file.txt", "w")
with open("Personaldetails_file.txt", "r") as f:
content = [x.strip('\n') for x in f.readlines()]
lines = f.readlines()
for line in lines:
words = line.split(",")
age = (line.split)(0)
weight = (line.split)(3)
height = (line.split)(4)
################################################################
totaldistancec = int(input("what was the total distance you cycled in KM?"))
totaltimec = int(input("how long did you cycle for in minutes?"))
calburn1 = (13.75 * weight) + (5 * height) - (6.67 * age)
speedc = totaldistancec/totaltimec
print ("on average you where running at a speed of", speedc, "KMph\nyou burnt", calburn1, " calouries")
totalc = (totaldistancec, totaltimec, speedc)
Cycling_file.write(str(totalc))
Cycling_file.close()
Personaldetails_file.close()
当我解开程序时出现错误。 线84,在TD calburn1 =(13.75 *重)+(5×高度) - (6.67 *年龄) UnboundLocalError:分配 之前引用局部变量 '重量' 没有人知道我该如何解决这个问题? 这是有关通过“#”包围了问题的代码未绑定本地错误,我不知道如何修复
答
您申报重量这里
def perd():
...
gender = input("Gender:")
weight = int(input("weight in kg:"))
height = int(input("height in cm:"))
...
,但你尝试在这个其他功能在这里使用它:
def tr():
....
calburn1 = (13.75 * weight) + (5 * height) - (6.67 * age)
....
这是一个单独的函数不知道perd()
函数中的变量,为什么要这样呢?它不需要它们中的任何一个来作为功能进行操作,以隔离代码段并且能够被自己使用。换句话说,一旦perd()
运行,其局部变量超出范围。看看这个问题,Short Description of the Scoping Rules?。要解决这个问题,你需要在perd()
全球范围内输入,不可取,因为它不是pythonic。或者,您可以将值作为函数参数如下所示:
td(weight, height, age):
#code here
现在您可以访问td函数中作为权重传入的值。但是你需要认识到这些变量不是同一个变量,它们可能共享相同的名称,但它们不一样。您将需要从perd函数中传入想要使用的所有值。这是可取的,因为您正在设计功能的方式可以使用模块化,所以可以处理来自用户的输入,而另一个可以对已知的有效数据执行计算,这使得更容易阅读代码,这是python的全部内容
在Python中,缩进是语法的一部分。请正确缩进您的代码。 – 2014-11-25 07:12:05
你如何调用你的函数td或perd? – 2014-11-25 07:13:20
如果你想'weight'变量与你初始设置为'int(input(“weight in kg:”))''相同,你需要把'global weight'作为函数体的第一行。否则python不知道你想要一个全局变量。 – Alec 2014-11-25 07:14:38