77.9k views
4 votes
How Do I Fix Unsupported Format String Passed To Nonetype.__format__ Here Is My Code: #Globals Name_list = [] #Keeps Tracks Of Names Score_list = [] #Keeps Track Of How do I fix unsupported format string passed to nonetype.__format__ student submitted image, transcription available below here is my code: #globals name_list = [] #keeps tracks of names score_list = [] #keeps track of scores status_list = [] #tracks grade_basis grade_list = [] #tracks grades based on the status_list def is_valid_graded(grade_basis): #validation bool return grade_basis == 1 or grade_basis == 0 def is_valid_score(score): #in range 0...100 validation bool return score >=0 and score <= 100 def compute_grade(score): #computes grades based on score and return grade if score > 80: grade = 'A' else: grade = 'B' return grade def compute_passfail(score,grade_basis): if grade_basis == 1: return compute_grade(score) if score >=40: grade = "Pass" else: grade = "Fail" def submit(): global name_list, score_list, status_list, grade_list name = input('Enter name?>>') score = int(input('Enter a score?>>')) if not is_valid_score(score): print('Score must be positive') grade_basis = int(input('Enter 1 for graded, 0 for pass/fail >>')) if not is_valid_graded(grade_basis): print('Enter 1 for graded, 0 for pass/fail') return grade = compute_passfail(score,grade_basis) name_list.append(name) score_list.append(score) if grade_basis == 1: status_list.append("Graded") else: status_list.append("PassFail") grade_list.append(grade) print(f'Name:{name} Score:{score:.2f}') def compute_average_score(): global score_list total_score = 0.0 num_inputs = 0 for score in score_list: total_score = total_score + score num_inputs = num_inputs + 1 if num_inputs > 0: avg = total_score / num_inputs else: avg = None return avg, num_inputs def summary(): average_score, num_inputs = compute_average_score() if average_score is not None: print(f'Average Score: {average_score:.2f}\tNumber of Inputs {num_inputs:d}') else: print('No Inputs to compute with!') def clear_lists(): global score_list, name_list, status_list, grade_list score_list.clear() name_list.clear() status_list.clear() grade_list.clear() def reset(): clear_lists() def line(): print('-'* 55) def display(): global score_list,name_list,status_list, grade_list if score_list: #strings, and things, when empty, are False. line() print(f'|{"Name":^12s}|{"Score":^12s}|{"Status":^14s}|{"Grade":^12s}|') line() for name,score,status,grade in zip(name_list, score_list, status_list,grade_list): print(f'|{name:12}|{score:12}|{status:14}|{grade:12}|') line() else: print('No Data!') quit = False while not quit: print('1.Submit 2.Summary 3.Reset 4.Display 5.Exit') choice = int(input('Enter choice: ')) if choice == 1: submit() elif choice == 2: summary() elif choice == 3: reset() print('Now ready for new series') elif choice == 4: display() elif choice == 5: clear_lists() quit = True else: print('Invalid Choice!')

1 Answer

5 votes

Final Answer:

The unsupported format string error is due to the attempt to format a non-string value using a format specifier that's incompatible. Specifically, attempting to format an integer as a float in the 'print' statement at the end of the 'submit' function causes this issue.

Step-by-step explanation:

In the 'submit' function, the line `print(f'Name:{name} Score:{score:.2f}')` tries to format 'score', which is an integer received from the user, as a floating-point number with two decimal places. However, the format specifier ':.2f' is designed for floating-point values, not integers.

To resolve this error, adjust the formatting to accommodate integers by removing the '.2f' formatting from the 'print' statement or by converting 'score' to a float using `float(score)` before formatting it in the print statement.

Additionally, ensuring proper validation and type-checking for user inputs, such as scores and grade basis, within the 'submit' function using 'is_valid_score()' and 'is_valid_graded()' functions respectively, can enhance the program's robustness. Implementing error handling mechanisms to prompt users for valid inputs in case of incorrect entries would also improve the overall user experience and program reliability.

Maintaining consistency in data types and using appropriate format specifiers in 'print' statements based on the data being processed helps avoid compatibility issues and prevents errors related to unsupported format strings.

Improving the code's documentation with comments and informative variable names could enhance readability and aid in identifying potential issues within the program, facilitating smoother debugging and maintenance processes.

User Ipeacocks
by
8.4k points