![](/img/trans.png)
[英]Python - How can I play the same sound multiple times without them interrupting each other?
[英]How can I get multiple values for my dictionary without them replacing each other?
def newRecord(record):
record= {'name' : 'test'}
students = int(input('How many students are in your class? '))
tests = int(input('How many tests have you given? '))
for name in range(students):
record ['name'] = input('Please enter students name: ')
for test in range(tests):
record ['test'] = input('Please enter test score: ')
print (record)
例如,根據用戶的輸入,我試圖獲取記錄以進行打印,如下所示,並且試圖將所有要測試的值保存在指定的名稱下。
{'name' : ' sam', 'test': '99','98','78'}
{'name' : 'john', 'test': '92','68','70'}
要么
{'sam': '99','98','78'}
{'john':'92','68','70'}
使用list
因為您的標簽暗示:
record['test'] = []
for test in range(tests):
score = input('Please enter test score: ')
record['test'].append(score)
您應該保留一個列表作為每個詞典關鍵字項的值,其中關鍵字將是學生姓名,而list
將包含測試成績。 請參閱下面的示例。
record= {}
students = int(input('How many students are in your class? '))
tests = int(input('How many tests have you given? '))
for name in range(students):
name = input('Please enter students name: ')
testScores = []
for test in range(tests):
testScores.append(input('Please enter test score: '))
record[name] = testScores
print (record)
它輸出:
How many students are in your class? 2
How many tests have you given? 3
Please enter students name: Alex
Please enter test score: 80
Please enter test score: 75
Please enter test score: 95
Please enter students name: Mac
Please enter test score: 90
Please enter test score: 88
Please enter test score: 79
{'Alex': ['80', '75', '95'], 'Mac': ['90', '88', '79']}
我在這里看到一些問題:
record
參數,這可能會導致一路奇怪 我的建議:
為Student
創建一個類,並在其中引用所有信息。
class Student(object): def __init__(self, name): self.name = name self.test_scores = []
在這里,我建議您定義一個合適的__str__()
值,但我將其留給讀者練習。
在某個地方創建一個記錄列表,它只是一個學生列表。
students = []
調整newRecord
方法以利用該類(並將其重命名以更好地描述其目的)。
def create_new_records(): students = int(input('How many students are in your class? ')) tests = int(input('How many tests have you given? ')) for i in range(students): student_name = input('Please enter students name: ') student = Student(student_name) for j in range(tests): student.test_scores.append(input('Please enter test score: ')) students.append(student) print(student)
您可以根據需要繼續使用字典:
def newRecord():
record = {}
students = int(input('How many students are in your class? '))
for name in range(students):
name = input('Please enter students name: ')
test = input('Please enter test score: ')
record[name] = test
print(record)
這將記錄:
How many students are in your class? 3
Please enter students name: john
Please enter test score: 12
Please enter students name: mike
Please enter test score: 33, 45, 66
Please enter students name: otherjohn
Please enter test score: 9
{'john': '12', 'otherjohn': '9', 'mike': '33, 45, 66'}
或者,您可以使用以下方法就地進行:
for name in range(students):
record[input('Please enter students name: ')] = input('Please enter test score: ')
print (record)
但是,這將首先要求您提供測試成績,然后是名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.