Question: How do I convert int to strings in my code? 1 and 0 to Graded and PassFail respectively. my code is pasted below. The user

How do I convert int to strings in my code? 1 and 0 to Graded and PassFail respectively. my code is pasted below. The user still inputs 1 and 0 but shows graded and PassFail in the terminal.

How do I convert int to strings in my code? 1 and

0 to Graded and PassFail respectively. my code is pasted below. The

#globals

students = {} #stores complete information about each student in a dictionary

#key: name value: [score, grade_basis, grade] score = value[0] grade_basis = value[1] grade = value[2]

#functions

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

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"

return grade

def submit():

global students

line = input('Enter name, score and grade basis (1 or 0)>>')

in_list = line.split()

name = in_list[0]

valid = False

while not valid:

score = float(in_list[1])

valid = is_valid_score(score)

if not valid: #validating score here

print('Error!')

print(score)

valid = False

while not valid:

grade_basis = int(in_list[2])

valid = is_valid_graded(grade_basis)

if not valid:

print('Error!') #validating grade basis here

print(grade_basis)

if grade_basis:

grade = compute_passfail(score,grade_basis)

else:

grade = compute_grade(score)

students[name] = [score,grade_basis,grade] #inserting record into dictionary

print(name,score,grade_basis,grade)

def compute_average_score():

global students

total_score = 0.0

num_inputs = 0

grade_basis_count = 0

for value in students.values():

score = value[0]

grade_basis = value[1]

grade = value[2]

total_score = total_score + score

num_inputs = num_inputs + 1

if grade_basis:

grade_basis_count = grade_basis_count + 1

if num_inputs > 0:

avg = total_score / num_inputs

else:

avg = None

return avg, num_inputs,grade_basis_count

def summary():

global students

totalA=0

totalB=0

total100=0

total0=0

average_score, num_inputs,grade_basis_count = compute_average_score()

if average_score is not None:

for key in students.keys():

value = students[key]

score = value[0]

grade = value[2]

if score==100:

total100=total100+1

if score==0:

total0=total0+1

if grade=='A':

totalA= totalA+1

if grade=='B':

totalB= totalB+1

print(f'Average Score: {average_score:.2f}\tNumber of Inputs: {num_inputs:d}\tGrade Basis Count: {grade_basis_count:d}\tCount of As: {totalA:d}\tCount of Bs: {totalB:d}\tCount of 100s: {total100:d}\tCount of 0s: {total0:d}')

else:

print('No Data!')

def clear_data():

global students

students.clear()

def reset():

clear_data()

def display():

global students

if students:

for key in students.keys():

value = students[key]

score = value[0]

grade_basis = value[1]

grade = value[2]

print(f'{key:12s}|{score:12.2f}|{grade_basis:14d}|{grade:12s}|')

else:

print('No data!')

def search():

global students

key = input('Enter a name to search for?>>')

if key in students.keys():

value = students[key]

score = value[0]

grade_basis = value[1]

grade = value[2]

print(f'{key:12s}|{score:12.2f}|{grade_basis:14d}|{grade:12s}|')

else:

print('No Key Found!')

def load():

global students

with open('inputfileHW6Q1.txt','r') as infile:

lines = infile.readlines()

for line in lines:

in_list = line.strip(' ').split()

name = in_list[0]

score = float(in_list[1])

grade_basis = int(in_list[2])

grade = compute_passfail(score,grade_basis)

students[name] = [score,grade_basis,grade]

def save():

global students

#write to a new file, from the dictionary

with open('outfileHW6Q1.txt','w') as outfile:

for name in students.keys():

value = students[name]

score = value[0]

grade_basis = value[1]

grade = value[2]

out_line = f'{name:s},{score:.2f},{grade_basis:d},{grade} '

outfile.write(out_line)

print('All Data Saved....')

#main

quit = False

while not quit:

print('1.Submit 2.Load 3.Summary 4.Save 5.Display 6.Search 7.Reset 8.Exit')

choice = int(input('Enter choice: '))

if choice == 1:

submit()

elif choice == 2:

load()

elif choice == 3:

summary()

elif choice == 4:

save()

elif choice == 5:

display()

elif choice == 6:

search()

elif choice == 7:

reset()

print('Data Cleared. Ready for new series')

elif choice == 8:

quit = True

clear_data()

else:

print('Invalid Choice!')

100.00 1 A 99.000 o Pass 80.00 1 B 39.000 o|Fail 88.00 11A Summary 4. Save 5.Display 6. Search 7. Reset 8.7 So, for both the displayed output from submit, and for inserting into the dictionary, you need to convert the land o to the strings 'Graded' and 'PassFail' respectively

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!