如何将列表返回到函数外的另一个列表

时间:2017-08-11 20:33:03

标签: python list function

我正在制作游戏。在这个游戏中,用户被问到一个问题。如果他做对了,它会进入right_answer列表并进入答案列表。如果他弄错了,它就会进入答案列表。加上他们得到一个正确的问题,他们得到正确的问题。它将成为8个问题,每个问题都是一个功能。这是第一个问题。问题是它没有打印任何东西。

def question_one(answer,right_answer,points):
answer = []
right_answer = []
print "how would you write this question in code so it would print"
answer=raw_input("< ")
#this is if he gets it right

if answer=="""print "how would you write this question in code so it would 
print""""":
#this is where the answers are inserted into the lists
    right_answer.append(answer)
# this is where the answer goes that they gave
    answer.append(answer)
    points=points+1
#this is if he gets it wrong
else:
    answer.append(answer)
#this is to return the values and the lists for the right answer and the 
answer
    return answer,right_answer,points    
(answer.append(answers),correct_answer.append(correct_answers), points)=question_one(answers,correct_answers,points)

1 个答案:

答案 0 :(得分:1)

我不是100%关于您正在尝试实施的内容,但这里有一个适合我的代码的近似值:

def question_one(answer, right_answer, points):
    '''
    The user is asked a question.
    If the answer is incorrect, it goes into the answer list.
    If the answer is correct, it goes into the right_answer list.
    '''
    answer_list = []
    right_answer = []
    print("What is 1 + 1?")
    answer = input()

    if answer == '2':
        right_answer.append(answer)
        answer_list.append(answer)
        points += 1
    else:
        answer_list.append(answer)

    return answer_list, right_answer, points