Python 程序在我打开时崩溃(CDM)。工作正常(空闲)
我刚刚完成python类的作业,由于某种原因,它无法在CDM中打开。当我处于空闲模式时,它会打开并正常运行。我的朋友代码看起来和我的一样,但他的可以在CDM中运行。我的车开得很快,撞得很快。我认为这是我的主要功能,因为如果它是我的其他功能,我将能够通过第一个输入。我和我的朋友查看了我的代码,没有发现任何错误,也不明白为什么会发生这种情况。既然我们是电脑迷,我想我会把它带到这里,是的,我的第一篇博文。有人能帮我找到我的错误吗Python 程序在我打开时崩溃(CDM)。工作正常(空闲),python,crash,Python,Crash,我刚刚完成python类的作业,由于某种原因,它无法在CDM中打开。当我处于空闲模式时,它会打开并正常运行。我的朋友代码看起来和我的一样,但他的可以在CDM中运行。我的车开得很快,撞得很快。我认为这是我的主要功能,因为如果它是我的其他功能,我将能够通过第一个输入。我和我的朋友查看了我的代码,没有发现任何错误,也不明白为什么会发生这种情况。既然我们是电脑迷,我想我会把它带到这里,是的,我的第一篇博文。有人能帮我找到我的错误吗 #Allen #CSC 110.09 #11/15/2014 #Home
#Allen
#CSC 110.09
#11/15/2014
#Home Work 8
def main(): #main
#The following should be displayed in the read file
#introduction
print('This program reads data from a file, calculate the statistics and then write the results to another file.')
print()
print('THIS PROGRAM WILL FIND:')
print()
print('• number of characters')
print('• number of letters')
print('• number of consonants')
print('• number of digits')
print('• number of white-space')
print('• number of word characters')
print('• number of punctuation characters')
print()
#flag
error_loop = False #making a loop so the user enter a valid file
while error_loop == False:
try:
original_file = input('Enter a file name: ') #user input a file to read
print()
old_file = open(original_file, 'r')
except Exception as nope:
print(original_file, 'not found. Please try again', nope)
print()
found = False
else:
error_loop = True
try:
characters_count = 0 #set counting to 0
letters_count = 0 #set counting to 0
consonants_count = 0 #set counting to 0
digits_count = 0 #set counting to 0
whitespaces_count = 0 #set counting to 0
wordcharacters_count = 0 #set counting to 0
punctuation_count = 0 #set counting to 0
#reading the file
read_file = old_file.read()
#counting the stats
while read_file != '':
#counting the characters
characters_count += characters(read_file)
#counting the letters
letters_count += letters(read_file)
#counting the consonants
consonants_count += consonants(read_file)
#counting digits
digits_count += digits(read_file)
#counting white spaces
whitespaces_count += whitespaces(read_file)
#counting word characters
wordcharacters_count += wordcharacters(read_file)
#counting punctuations
punctuation_count += punctuation(read_file)
#read the file again
read_file = old_file.read()
except Exception as nope:
print()
print(nope)
else:
#making the new file
new_file_name = input("please enter a new file name: ") #user can rename the new file
new_file = open(new_file_name + ".txt",'w') #call the new file
new_file.write('Statistics for ')
new_file.write(original_file)
new_file.write(':\n')
new_file.write('\tCharacters: ')
new_file.write(str(characters_count)+'\n')
new_file.write('\tLetters: ')
new_file.write(str(letters_count)+'\n')
new_file.write('\tConsonants: ')
new_file.write(str(consonants_count)+'\n')
new_file.write('\tDigits: ')
new_file.write(str(digits_count)+'\n')
new_file.write('\tSpaces: ')
new_file.write(str(whitespaces_count)+'\n')
new_file.write('\tWord characters: ')
new_file.write(str(wordcharacters_count)+'\n')
new_file.write('\tPunctuation:')
new_file.write(str(punctuation_count)+'\n')
finally:
#closes the file
old_file.close()
new_file.close()
print()
print("we have created a new file called", new_file_name)
def characters(characters): #function 1
return len(characters) #length of characters
def letters(letter): #function 2
letters_total = 0
letters = ['A', 'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q',\
'R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h',\
'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
#list of the alphabeth
for line in letter:
if line in letters:
letters_total += 1
return letters_total
def consonants(consonant): #function 3
consonants_total = 0
consonants = ['B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V',\
'W','X','Y','Z','b','c','d','f','g','h','j','k','l','m','n','p','q',\
'r','s','t','v','w','x','y','z'] #list of consonats
for line in consonant:
if line in consonants:
consonants_total += 1
return consonants_total
def digits(digit): #function 4
digits_total = 0
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'] #list of numbers
for line in digit:
if line in digits:
digits_total += 1
return digits_total
def whitespaces(whitespace): #function 5
whitespaces_total = 0
search = ' ' # space
for line in whitespace:
if line == search:
whitespaces_total += 1
return whitespaces_total
def wordcharacters(wordcharacter): #function 6
wordcharacters_total = 0
wordcharacters = ['@','#','$','%','&','+','-','=','<','>','*','/'] #list of word characters
for line in wordcharacter:
if line in wordcharacters:
wordcharacters_total += 1
return wordcharacters_total
def punctuation(punctuations): #function 7
punctuation_total = 0
punctuation = ['!','~','`','^','(',')','_','{','}',\
'[',']','|','\\',';',':','"',"'",',','.','?'] #list of punctuation
for line in punctuations:
if line in punctuation:
punctuation_total += 1
return punctuation_total
main() #Call out main
艾伦
#CSC 110.09
#11/15/2014
#家庭作业8
def main():#main
#读取文件中应显示以下内容
#导言
print('此程序从文件中读取数据,计算统计数据,然后将结果写入另一个文件')
打印()
打印('此程序将找到:')
打印()
打印(“•字符数”)
打印(“•字母数”)
打印(“•辅音数”)
打印(“•位数”)
打印(“•空格数”)
打印(“•单词字符数”)
打印(“•标点字符数”)
打印()
#旗
错误_loop=False#进行循环以便用户输入有效文件
当错误_loop==False时:
尝试:
原始文件=输入('输入文件名:')#用户输入要读取的文件
打印()
旧文件=打开(原始文件“r”)
除不适用的例外情况外:
打印(原始文件“未找到。请重试”,否)
打印()
发现=错误
其他:
错误\u循环=真
尝试:
字符计数=0#将计数设置为0
字母计数=0#将计数设置为0
辅音_count=0#设置计数为0
数字计数=0#将计数设置为0
空格_count=0#将计数设置为0
wordcharacters_count=0#将计数设置为0
标点符号计数=0#将计数设置为0
#读取文件
read_file=旧的_file.read()
#统计数据
读取_文件时!='':
#数字
字符数+=字符(读取文件)
#数信
字母数+=字母(读取文件)
#计算辅音
辅音\u计数+=辅音(读取\u文件)
#数数
位数\u计数+=位数(读取\u文件)
#计算空白
空白\u计数+=空白(读取\u文件)
#计算字字符
wordcharacters\u count+=wordcharacters(读取文件)
#计算标点符号
标点符号计数+=标点符号(读取文件)
#再次读取该文件
read_file=旧的_file.read()
除不适用的例外情况外:
打印()
打印(否)
其他:
#制作新文件
new_file_name=input(“请输入新文件名:”)#用户可以重命名新文件
new_file=open(new_file_name+“.txt”,“w”)#调用新文件
新建_file.write('Statistics for')
新建\u文件。写入(原始\u文件)
新建文件。写入(“:\n”)
新建文件。写入('\t字符:')
新建文件。写入(str(字符数)+'\n')
新建文件。写入('\tLetters:')
新建文件。写入(str(字母计数)+'\n')
新建\u文件。写入('\t注释:')
新建\u文件。写入(str(辅音\u计数)+'\n')
新建文件。写入('\tDigits:')
新建文件。写入(str(数字计数)+'\n')
新建_文件。写入('\t空间:')
新建文件。写入(str(空格计数)+'\n')
新建文件。写入('\t第二个字符:')
新建文件。写入(str(字字符数)+'\n')
新建\u文件。写入('\tpuntuation:')
新建文件。写入(str(标点符号计数)+'\n')
最后:
#关闭文件
旧的_文件。关闭()
新建_文件。关闭()
打印()
打印(“我们已经创建了一个名为”new_file_name“的新文件)
定义字符(字符):#函数1
返回长度(字符)#字符长度
def字母(字母):#功能2
字母总数=0
字母=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'\
‘R’、‘S’、‘T’、‘U’、‘V’、‘W’、‘X’、‘Y’、‘Z’、‘a’、‘b’、‘c’、‘d’、‘e’、‘f’、‘g’、‘h’\
‘i’、‘j’、‘k’、‘l’、‘m’、‘n’、‘o’、‘p’、‘q’、‘r’、‘s’、‘t’、‘u’、‘v’、‘w’、‘x’、‘y’、‘z’]
#字母表
对于字母中的行:
如果是字母行:
字母总数+=1
回信总数
def辅音(辅音):#功能3
辅音总数=0
辅音=['B'、'C'、'D'、'F'、'G'、'H'、'J'、'K'、'L'、'M'、'N'、'P'、'Q'、'R'、'S'、'T'、'V'\
‘W’、‘X’、‘Y’、‘Z’、‘b’、‘c’、‘d’、‘f’、‘g’、‘h’、‘j’、‘k’、‘l’、‘m’、‘n’、‘p’、‘q’\
'r'、's'、't'、'v'、'w'、'x'、'y'、'z']#联合体列表
对于辅音行:
如果是辅音行:
辅音_总计+=1
返回辅音
def数字(数字):#功能4
总位数=0
数字=['0','1','2','3','4','5','6','7','8','9','0']#数字列表
对于以数字表示的行:
如果是数字行:
总位数+=1
返回数字总数
def空白(空白):#函数5
空格总数=0
搜索=''#空格
对于空格中的行:
如果行==搜索:
空格_总计+=1
返回空格\u总计
def wordcharacters(wordcharacter):#函数6
wordcharacters\u总计=0
wordcharacters=['@'、'#'、'$'、'%'、'&'、'+'、'-'、'='、'*'、'/']#单词字符列表
对于wordcharacter中的行:
如果行为wordcharacters:
字字符总数+=1
返回字字符总数
def标点符号(标点符号):#功能7
标点符号总数=0
标点符号=['!'、'~'、'''、'^'、'('、')、'、'{'、'}'\
“[”、“]”、“|”、“\\”、“;”、“:”、““、”、“、”、“、”、“、”、“?”]#标点符号列表
对于标点符号中的行:
如果标点符号中有行:
标点符号总数+=1
返回标点符号\u总计
main()#调出main
问题在于def main()块后面的缩进 你有
def main(): #main
#The following should be displayed in the read file
#introduction
print('This program reads data from a file, calculate the statistics and then write the results to another file.')
print()
print('THIS PROGRAM WILL FIND:')
...
您应该在定义main()方法后缩进这些print语句
def main():
print('indent after definition is started like this')
我不知道这是否是您的代码的唯一问题。请尝试更改它,如果您有更多问题,请返回