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” 有人知道我怎样才能纠正这个错误吗? 与被“#”包围的问题相关的代码
你在这里申报重量
但是你试着用它来做另一个函数:
这是一个单独的函数,它不知道
perd()
函数中的变量,为什么要这样做?它不需要它们中的任何一个来操作,因为函数应该隔离代码片段并能够自己使用。换句话说,一旦perd()
运行,它的局部变量就会超出范围。看看这个问题,Short Description of the Scoping Rules?。要解决这个问题,您需要将perd()
中的输入设置为全局的,这是不可取的,因为它不是非常pythonic的。或者可以将值作为函数参数传入,如下所示:现在您可以在td函数中访问作为weight传入的值。但是你需要意识到这些变量是不同的,它们可能有相同的名字,但是它们不一样。您需要从perd函数中传入所有要使用的值。这是最好的,因为您设计的函数可以模块化使用,因此一个函数可以处理从用户获取的输入,而另一个函数可以对它已经知道的有效数据执行计算,这使得代码更易于阅读,这就是python所要做的
相关问题 更多 >
编程相关推荐