[Python] How to use dictionary. (I'm a beginner) (T)

Asked 2 years ago, Updated 2 years ago, 32 views

file=open("python_07_04.txt", 'w')

subject="Q"
score="1"

test_dict={subject:score}

while(subject!="Q"):
     subject=input()
while(score!="0"):
     score=input()

infile.write(subject+" : "+score)
infile.write("total = "+score)
infile.write("average = "+float(score/3))

infile.close()
outfile=open("python_07_04.txt",'r')
text=outfile.read()
print(text)
outfile.close()

I coded the picture above, but it doesn't proceed from one to ten Please advise me how to set up dictionary. Thank you.

python python3

2022-09-22 17:56

1 Answers

infile=open("python_07_04.txt",'w')

test_dict={'Q':1}

while(test_dict['Q']==1):
    subject=input()
    score=input()
    test_dict[subject]=score


test_dict.pop('Q',None)
total = 0

for a in test_dict:
    infile.write(str(a+' : '+test_dict[a]+'\n'))
    total+=float(test_dict[a])

infile.write(str("total = {}\n".format(total)))
infile.write(str('avg = {}\n'.format(total/(len(test_dict)))))

infile.close()
outfile=open("python_07_04.txt",'r')
text=outfile.read()
print(text)

outfile.close()


2022-09-22 17:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.