Python 3.x Python:for-key-in-gascoount:name错误:name';煤气量';没有定义 导入字符串 def getFile(): filename=input('filename:')#文件名应为.csv 文件=打开(文件名“
Python:for-key-in-gascoount:name错误:name';煤气量';没有定义Python 3.x Python:for-key-in-gascoount:name错误:name';煤气量';没有定义 导入字符串 def getFile(): filename=input('filename:')#文件名应为.csv 文件=打开(文件名“,python-3.x,Python 3.x,Python:for-key-in-gascoount:name错误:name';煤气量';没有定义 导入字符串 def getFile(): filename=input('filename:')#文件名应为.csv 文件=打开(文件名“r”) firstline=True Line=file.readline() 如果行==无或行=='': 一无所获 if firstline:#我不想读取字段名 Line=file.readline()#还有更多内容需要阅读 firstlin
导入字符串
def getFile():
filename=input('filename:')#文件名应为.csv
文件=打开(文件名“r”)
firstline=True
Line=file.readline()
如果行==无或行=='':
一无所获
if firstline:#我不想读取字段名
Line=file.readline()#还有更多内容需要阅读
firstline=False,所以我跳过了。代码假定
返回文件
#计算字段中的(T)个数[正确]
def calcT(正确):
全局计数
发现=错误
对于ch in correct:#依次查看每个字符
如果输入“tT”:
找到=真
如果发现:
t计数+=1
#字段中最不常见的字符串出现多少次[气体]
def气体计数(气体):
全球煤气量
如果气体中的气体计数:
气体计数[气体]+=1
其他:
气体计数[气体]=1
最小值=100000
对于键入气体计数:
如果气体计数[键]<最小值:
最小=气体计数[键]
答案=[]
对于键入气体计数:
如果gasCount[键]==最小值:
答案。追加(键)
#查找字段[quant]中的值之和小于(408)
定义和数量(数量):
全球qsum
如果数量<408:
qsum+=quant
代码=“0”
#“代码”字段中有多少值与格式9999(x9+)9不匹配?
def检查字符串(astring):
如果len(代码)!=10:
返回错误
如果未在string.digits中搜索[0]:
返回错误
如果未在string.digits中搜索[1]:
返回错误
如果未在string.digits中搜索[2]:
返回错误
如果未在string.digits中搜索[3]:
返回错误
如果不使用astring[4]=='(':
返回错误
如果不是string.ascii_小写形式的[5]:
返回错误
如果未将[6]括在string.digits中:
返回错误
如果不使用astring[7]=='+':
返回错误
如果不使用astring[8]==')':
返回错误
如果未在string.digits中搜索[9]:
返回错误
返回真值
如果选中字符串(代码):
全球帐户
帐户+=1
#(30)和(107)范围内[age]字段中数字的平均值是多少
def平均年龄(年龄):
全球舞台
全球年龄统计
如果年龄>=30,年龄=2.482,并且长得很好,您(似乎)永远不会初始化气体计数。但如果这是第58行,您没有向我们显示完整的代码,所以很难说。即使您使用了global
,您也必须在代码中的某个地方有一行类似于gascoount={}
,才能将其初始化为空字典。初始化它是什么意思?对不起,我是新来的!我会用完整的代码编辑主帖子OK我编辑了主帖子下面的两个循环minimate=
和answers=
应该是它们上面定义的函数的一部分吗?错误真的出现在第63行上了吗?还是你从那以后更改了内容?
import string
def getFile():
filename = input('Filename: ') #the file name should be .csv
file = open(filename, 'r')
firstline = True
Line = file.readline()
if Line == None or Line == '':
return None
if firstline: # I do not want to read the field names
Line = file.readline() # there is more to read
firstline = False # so I skip them. the code assuems
return file
#Count the number of (T's) in the field [correct]
def calcT(correct):
global tCount
found = False
for ch in correct:#look at each character in turn
if ch in 'tT':
found = True
if found:
tCount +=1
#How many times does the least common string appear in the field [gas]
def gas_Count(gas):
global gasCount
if gas in gasCount:
gasCount[gas] += 1
else:
gasCount[gas] = 1
smallest = 100000
for key in gasCount:
if gasCount[key] < smallest:
smallest = gasCount[key]
answers = []
for key in gasCount:
if gasCount[key] == smallest:
answers.append(key)
#Find the sum of the values in the field [quant] less than (408)
def sum_quant(quant):
global qsum
if quant < 408:
qsum += quant
code="0"
#How many values in the 'code' field do not match the format 9999(x9+)9?
def checkString(astring):
if len(code) != 10:
return False
if not astring[0] in string.digits:
return False
if not astring[1] in string.digits:
return False
if not astring[2] in string.digits:
return False
if not astring[3] in string.digits:
return False
if not astring[4]=='(':
return False
if not astring[5] in string.ascii_lowercase:
return False
if not astring[6] in string.digits:
return False
if not astring[7]=='+':
return False
if not astring[8]==')':
return False
if not astring[9] in string.digits:
return False
return True
if checkString(code):
global cCount
cCount += 1
#What is the average value of the numbers in the field [age] in the range (30) and (107) inclusive
def average_age(age):
global tAge
global ageCount
if age >= 30 and age <=107:
tAge += age
ageCount += 1
#Find the sum of the numbers in field [length] between (2.482) and (6.428) inclusive
def sum_Length(leng):
global lensum
if leng >= 2.482 and leng <= 6.428:
lensum += leng
#count the lines where gas's have the value (Nitrogen) *or* quant is less than 318
def calcGas(gas, quant):
global clines
if gas == 'Nitrogen' or quant < 318:
clines += 1
def processLine(Line):
Line = Line.strip()
fields = Line.split(',')
correct = fields[0]
gas = fields[1]
quant = int(fields[2])
code = fields[3]
age = int(fields[4])
leng = float(fields[5])
calcT(correct)
sum_Length(leng)
calcGas(gas, quant)
average_age(age)
sum_quant(quant)
gas_Count(gas)
checkString(astring)
def processFile(data):
for line in data:
processLine(line)
data.close()
def displayResults():
#Count the number of (T's) in the field [correct]
print('The number of (T) in the field [correct]: %d'%(tCount))
print('-' *10)
print(gasCount)
print('-' *10)
print(cCount)
#Find the sum of the values in the field [quant] less than (408)
print('The sum of the values in the field [quant] less than (408): %d'%(qsum))
print('-' *10)
#How many values in the 'code' field do not match the format 9999(x9+)9?
#print('The values in the code field do not match the format 9999(x9+)9: %d'%()
print('-' *10)
#What is the average value of the numbers in the field [age] in the range (30) and (107) inclusive
print((tAge/ageCount))
print('-' *10)
#Find the sum of the numbers in field [length] between (2.482) and (6.428) inclusive
print('The sum of the numbers in field [length] between (2.482) and (6.428): %6.3f'%(lensum))
print('-' *10)
#count the lines where gas's have the value (Nitrogen) *or* quant is less than 318
print('The lines where gas have the value (Nitrogen) *or* quant is less than 318: %d' %(clines))
tCount = 0
gasCount = {}
qsum = 0
cCount = 0
ageCount = 0
tAge = 0
lensum = 0
clines = 0
myfile = getFile()
processFile(myfile)
displayResults()
Traceback (most recent call last):
File "C:\Users\Ultra\Downloads\3215402\3215402\3215402 Stage 1 Function.py", line 63, in <module>
def gas_Count(gas):
global gasCount
if gas in gasCount:
gasCount[gas] += 1
else:
gasCount[gas] = 1
smallest = 100000
for key in gasCount:
if gasCount[key] < smallest:
smallest = gasCount[key]
answers = []
for key in gasCount:
if gasCount[key] == smallest:
answers.append(key)
Traceback (most recent call last):
File "C:\Users\Ultra\Downloads\3215402\3215402\3215402 Stage 1 Function.py", line 57, in <module>
for key in gasCount:
NameError: name 'gasCount' is not defined