0
我剛剛完成了我的Python課的作業,並且出於某種原因無法在CDM中打開作業。它在我處於空閒模式時打開並運行良好。我的朋友代碼看起來和我的一樣,但他可以在CDM中運行。我的開放和崩潰真的很快。我認爲這是我主要的東西,因爲如果這是我的其他功能,我將能夠通過第一個輸入。我的朋友和我查看了我的代碼,發現沒有錯誤並且困惑爲什麼會發生這種情況。既然我們是電腦菜鳥,但我會把它帶到這裏,是的,我的第一個帖子大聲笑。有人能幫我找到我的錯誤嗎?當我打開它時(CDM),程序崩潰。在(IDLE)中工作正常
#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