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:赋值前引用的局部变量'weight' 有谁知道如何解决这个错误? 与'#'
包围的问题相关的代码答案 0 :(得分:1)
你在这里声明重量
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的全部内容