Question: Python code troubles The following code compiles but when it reaches the write() function it returns an error saying: NameError: name 'wordcount' is not defined
Python code troubles
The following code compiles but when it reaches the write() function it returns an error saying: NameError: name 'wordcount' is not defined
It does this in the last line of code for the arguments passed into write(). Any suggestions?
def calc():
wordcount = 0 charcount = 0 readf = input("Please enter the name of the file to be read:") if readf != "lab_assignment_1_Q5_test_data.txt": print("File does not exist!") else: file = open(readf,"r") f = file.read().upper() for word in f.split(): wordcount += 1 charcount += len(word) acount = f.count("A") bcount = f.count("B") ccount = f.count("C") dcount = f.count("D") ecount = f.count("E") aper = (acount/charcount)*100 bper = (bcount/charcount)*100 cper = (ccount/charcount)*100 dper = (dcount/charcount)*100 eper = (ecount/charcount)*100 letter = (acount,bcount,ccount,dcount,ecount) percent = (aper,bper,cper,dper,eper) return wordcount return charcount return letter return percent def write(wordcount,charcount,letter,percent): writef = input("Please enter the name of the file to write the statistics to:") newfile = open(writef,"x") newfile.write("#Juan Inzunza #CSE 208 Lab #1, Question 5","a") newfile.write("#Test Data Result","a") newfile.write("Letter Occurences Percentage of non-white space characters","a") newfile.write("a", letter[0], percent[0],"%","a") newfile.write("b", letter[1], percent[1],"%","a") newfile.write("c", letter[2], percent[2],"%","a") newfile.write("d", letter[3], percent[3],"%","a") newfile.write("e", letter[4], percent[4],"%","a") newfile.write("Total number of non-whitespace characters:",charcount,"a") newfile.write("Total number of words:",wordcount,"a")
calc() write(wordcount,charcount,letter,percent)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
