Question
file = input(Enter the name of the score file: ) print(Contestant score:) try: ftest = open(file, 'r') response = passed except FileNotFoundError: print(There was an
file = input("Enter the name of the score file: ") print("Contestant score:") try: ftest = open(file, 'r') response = "passed" except FileNotFoundError: print("There was an error in reading the file.") gamer_dict = {} with open(file, "r") as infile: for row in infile: splits = row.split(" ") name = splits[0].strip() score = int(splits[1].strip()) if name not in gamer_dict: gamer_dict[name] = score else: gamer_dict[name] += score for k, v in sorted(gamer_dict.items()): print(k, v)
I need to add this to the program above:
If the file cannot be opened for reading, the program prints an error message:
There was an error in reading the file.
If the file contains a line that does not have two strings separated by spaces/spaces, the program prints an error message:
There was an erroneous line in the file:
and the contents of the incorrect line to the next line.
If the file contains a line whose last string cannot be interpreted as an integer, the program prints an error message:
There was an erroneous score in the file:
and for the next a string that was not interpretable as an integer.
The program processes the file line by line from beginning to end. The program prints an error message for the first error it finds. The execution of the program ends immediately after printing the error message. This means that even if there are several errors in the file, the program prints only one error message, and not besides the error message, nothing else is printed.
I got this code earlier from here:
file = input("Enter the name of the score file: ") readERROR = 0 fileERROR = 0 try: ftest = open(file, 'r') response = "passed" except FileNotFoundError: print("There was an error in reading the file.") fileERROR = 1 gamer_dict = {} if not fileERROR: with open(file, "r") as infile: for row in infile: splits = row.split(" ") if len(splits) != 2: print("There was an erroneous line in the file: ") print(row) readERROR = 1 break name = splits[0].strip() try: score = int(splits[1].strip()) except: print("There was an erroneous score in the file: ") print(splits[0].strip()) readERROR = 1 break score = int(splits[1].strip()) if name not in gamer_dict: gamer_dict[name] = score else: gamer_dict[name] += score if not readERROR and not fileERROR: print("Contestant score:") for k, v in sorted(gamer_dict.items()): print(k, v)
But this comes up with these errors:
Input and Output Tests / Sytt- ja tulostustestit Tests passed / Lpistyj testej: 1/3 (33\%) In the following test case reports the red color is used to represent wrong output lines from your program. Seuraavissa testitapausten tuloksissa punaista vri on kytetty kuvaamaan ohjelmasi tuottamaa virheellist tulosterivi. The special symbol ' ' is used to represent a single whitespace character ' ' in your program's output. Erikoissymbolia '' on kytetty kuvaamaan yht ohjelmasi tulosteessa esiintynytt vlilyntimerkki ' '. Test case 02 / Testitapaus 02 Second case; erroneous line in the file Toinen testitapaus: virheellinen rivi tiedostossa et Test case 03 / Testitapaus 03 Third case; the score seems not to be an int Kolmas tapaus: pisteet eivt ole kokonaislukujaStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started