Question: I am facing 2 problems with this Python program: 1)First it throwing an Invalid Syntax error in the 2nd if condition (if fileName == na
I am facing 2 problems with this Python program:
1)First it throwing an "Invalid Syntax" error in the 2nd if condition (if fileName == "na na boo boo":). It's not accepting the exit() function call
2) If I comment out the exit() call, it runs but there is a logical/semantics problem. For Example if I type "na na boo boo" as the file name, it prints the desired message but it also runs the
code under the except block as in:
Enter the file name (Ex: file1.txt) or type 0 to quit: na na boo boo NA NA BOO BOO TO YOU - You have been punk'd!
Error opening file. Invalid file name Make sure the file and this program are in the same directory Enter the correct file name with a .txt (Ex: file.txt) Enter the file name (Ex: file1.txt) or type 0 to quit:
______________________________________________________________________________________________________________________________________________
""" Exercise 3: Sometimes when programmers get bored or want to have a bit of fun, they add a harmless Easter Egg to their program. Modify the program that prompts the user for the file name so that it prints a funny message when the user types in the exact file name "na na boo boo". The program should behave normally for all other files which exist and don't exist. Here is a sample output: Enter the file name: na na boo boo NA NA BOO BOO TO YOU - You have been punk'd!
Enter the file name: mbox.txt Error opening file. Invalid file name
Enter the file name: mbox.txt There were 27 lines starting with X-DSPAM-Confidence: Average spam confidence: 0.7507185185185187
Exercise 2: Write a program to prompt for a file name, and then read through the file and look for lines of the form: X-DSPAM-Confidence: 0.8475 When you encounter a line that starts with "X-DSPAM-Confidence:" pull apart the line to extract the floating-point number on the line. Count these lines and then compute the total of the spam confidence values from these lines. When you reach the end of the file, print out the average spam confidence.""" #Make sure the file and this program are in the same directory while True: fileName = input("Enter the file name (Ex: file1.txt) or type 0 to quit: ") if fileName == "0": quit() # Or you can use exit() if fileName == "na na boo boo": { print("NA NA BOO BOO TO YOU - You have been punk'd! ") exit() # INVALID SYNTAX ERROR ON THIS LINE. PLEASE HELP }
try: fileHandle = open(fileName) except: print("Error opening file. Invalid file name " "Make sure the file and this program are in the same directory " "Enter the correct file name with a .txt (Ex: file.txt)") continue
count = 0 total = 0 for line in fileHandle: line = line.rstrip() if line.startswith("X-DSPAM-Confidence:"): floatValue = float(line.split(":")[1].strip()) total = total + floatValue count = count + 1
print("There were ",count, " lines starting with X-DSPAM-Confidence:") average = total / count print("Average spam confidence: ", average)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
