I want to add my name and score to the list and display them all together at the end.

Asked 1 years ago, Updated 1 years ago, 386 views

I am writing a code that allows me to add my name and score to the list in python.
The code below is a test code that adds your name and score to the list and tries to exit the code at any time, so it works fine.However, I would like to write a code that prints the name and score together at the end of the code I want to write, but what kind of changes should I make?I look forward to hearing a lot of advice.

Current Code

def listofname():
    members = [ ]
    scores=[ ]
    done = False
    while done!= True:
        Students = input ("Enter a name or enter 'done' when finished")
        ifStudents=="done":
            done = True
        
        else:
            members.append({"name":Students})
            Score=input ("Enter score")
            scores.append({"Score":Score})
        
        print(members)
        print(scores)

print(listofname())

Printed data

[{'name':'Tanaka'}, {'name':'Yamada'}, {'name':'Kojima'}]
[{'Score':'70'}, {'Score':'20'}, {'Score':'80'}]

Printed data

 [{'name':Tanaka, Yamada, Kojima}]
[{'Score': 70, 20, 80}]

If possible, I would appreciate it if you could tell me how to print the number of names added to the list and how to write the highest, lowest, average, and code that can be printed.

Sorry for the long letter

python

2022-12-17 14:53

1 Answers

The expected output in the questionnaire is a syntax error.

 [{'name':Tanaka, Yamada, Kojima}]
[{'Score': 70, 20, 80}]

The following code lists the values using name, Score as the key in the dictionary.
※ The int(Score) portion may generate a ValueError.

def listofname():
    members,scores={'name':[]}, {'Score':[]}
    while1:
        Students = input ("Enter a name or enter 'done' when finished")
        ifStudents=='done':break
        members ['name'] + = Students
        Score=input('Enter score')
        scores ['Score'] + = [int(Score)]
        
    print(members)
    print(scores)

if__name__=='__main__':
    listofname()

# {'name':['Tanaka', 'Yamada', 'Kojima']}
# {'Score': [70, 20, 80]}


2022-12-18 03:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.