-3
我是新來的編程,我想從一個函數返回一個字典,並在另一個使用它,但我一直得到這個相同的錯誤,標題。這裏是我的代碼:TypeError:take_quiz()缺少1所需的位置參數:'數據庫'
def read_qa(filename = 'data.csv'):
database = {}
f_in = open(filename, 'r')
for line in f_in:
line_list = line.strip().split(',')
question = line_list[0]
answers = line_list[1:]
database[question] = answers
f_in.close()
return database
def take_quiz(database):
name = input("Enter your name: ")
num_questions = eval(input("How many questions?: "))
if num_questions < len(database):
print('The quiz will have', num_questions, 'questions.')
else:
print('The quiz will have', len(database), 'questions.')
print()
read_qa()
take_quiz()
Error:
Traceback (most recent call last):
File "/Users/BA/PycharmProjects/p/b.py", line 22, in <module>
take_quiz()
TypeError: take_quiz() missing 1 required positional argument: 'database'
Process finished with exit code 1
您是否嘗試將第一個函數的返回值傳遞給第二個函數? –
您可以先閱讀[Python教程和函數](https://docs.python.org/3/tutorial/controlflow.html#defining-functions);你的'take_quiz()'函數需要傳入一些東西。它看起來像你的'read_qa()'函數*返回*你可以傳遞的東西。 –
所以'database = read_qa()',然後'take_quiz(數據庫)' –