def readFromFile():
clientID = input("Enter your clientID: ")
clientFile = open("clientIntensity.txt","r")
for record in clientFile:
if record.startswith(clientID):
print ("Your intensity is", record[6:14])
intensity = record[6:10]
if intensity == "High":
print(" Running n Swimming n Aerobics n Football n Tennis")
else:
print(" Walking n Hiking n Cleaning n Skateboarding n Basketball")
clientFile.close()
readFromFile()
def Time():
Running = int(input("Please enter the ammount of minutes you spent Running: "))
Swimming = int(input("Please enter the ammount of minutes you spent Swimming: "))
Aerobics = int(input("Please enter the ammount of minutes you spent doing Aerobics: "))
Football = int(input("Please enter the ammount of minutes you spent playing Football: "))
Tennis = int(input("Please enter the ammount of minutes you spent playing Tennis: "))
Time()
print(clientID)
print("The ammount of time spent exercising this week is",Running+Swimming+Aerobics+Football+Tennis)
这是我拥有的代码,我不断收到语法错误:
Traceback (most recent call last):
File "C:UsersUserDesktopControlled AssessmentRecorder.py", line 26, in <module>
print(clientID)
NameError: name 'clientID' is not defined:
谁能帮忙?我需要它,以便它将打印用户先前在代码开头输入的clientID。也可以有人帮助使用验证技术来制作它,以便用户只能为活动变体输入 0 到 120 之间的数字,例如"正在运行"
clientId = ""
Running = -1
Swimming = -1
Aerobics = -1
Football = -1
Tennis = -1
def readFromFile():
global clientID
clientID = input("Enter your clientID: ")
clientFile = open("clientIntensity.txt","r")
for record in clientFile:
if record.startswith(clientID):
print ("Your intensity is", record[6:14])
intensity = record[6:10]
if intensity == "High":
print(" Running n Swimming n Aerobics n Football n Tennis")
else:
print(" Walking n Hiking n Cleaning n Skateboarding n Basketball")
clientFile.close()
readFromFile()
def my_input(event):
num = -1
while not 0 <= num <= 120:
num = int(input("Please enter the ammount of minutes you spent " + event + ": "))
return num
def Time():
global Running, Swimming, Aerobics, Football, Tennis
Running = my_input('Running')
Swimming = my_input("Swimming")
Aerobics = my_input("Aerobics")
Football = my_input("Football")
Tennis = my_input("Tennis")
Time()
print(clientID)
print("The ammount of time spent exercising this week is",Running+Swimming+Aerobics+Football+Tennis)
clientId
是在函数readFromFile
的本地命名空间中定义的。
然后,尝试在全局命名空间中使用 print
调用它,而全局命名空间不存在它。
最好将clientId返回到变量中,如下所示:
def readFromFile():
clientID = input("Enter your clientID: ")
with open("clientIntensity.txt","r") as clientFile:
for record in clientFile:
if record.startswith(clientID):
print ("Your intensity is", record[6:14])
intensity = record[6:10]
if intensity == "High":
print(" Running n Swimming n Aerobics n Football n Tennis")
else:
print(" Walking n Hiking n Cleaning n Skateboarding n Basketball")
return clientID
clientID = readFromFile()
... # your time function here
print(clientID)
这将按原样运行代码,但将clientID返回给变量,允许在全局命名空间中声明它(作为您将其返回的任何变量,在这种情况下仍然clientID
)。然后可以将其传递给打印。
还值得注意的是,您读取文件的方式是非pythonic的,最好使用with
子句,这比使用file.open()
更安全(我在上面的代码中使用了with
子句)
@MikeStainer。在代码开头添加以下内容:
clientId = ""
Running = -1
Swimming = -1
Aerobics = -1
Football = -1
Tennis = -1
在readFromFile
函数的开头,添加global clientId
在Time
函数的开头,添加 global Running, Swimming, Aerobics, Football, Tennis
。这允许函数访问全局变量。
如果要强制使用 0 到 120 之间的值,只需使用 while 循环,重新分配变量,直到它们满足这样的约束。